.dll和.lib的区别
lib是静态库,dll一般是动态链接库(也有可能是别的)
比如要编译个exe,lib在编译的时候就会被编译到exe里,作为程序的一部分
而dll是不被编译进去,是运行的时候才调入的(可能是exe刚运行就调入,也可能运行了一半才调入)
用法,lib需要个.lib文件和一个.h文件,程序正常使用.h的函数,在链接选项里加入.lib文件就ok
dll用法有2种,一种是 .h + .lib + .dll的,用法和前面一样,中间的lib是个中转,运行的时候会调用dll
还有就是直接用dll,需要知道dll的函数定义,用LoadLibrary和GetProcAddress把函数指针取出来,看msdn
的例子吧
---------------------------------------------------------------------------------------------
stdafx.h、stdafx.cpp是干什么用的?为什么我的每一个cpp文件都必须包含stdafx.h?
Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编译程序也要花费相当长的时间来
完成工作。由于每个.CPP文件都包含相同的include文件,为每个.CPP文件都重复处理这些文件就显得很傻了
。
为避免这种浪费,AppWizard和Visual C++编译程序一起进行工作,如下所示:
◎AppWizard建立了文件stdafx.h,该文件包含了所有当前工程文件需要的MFC include文件。且这一文
件可以随被选择的选项而变化。
◎AppWizard然后就建立stdafx.cpp。这个文件通常都是一样的。
◎然后AppWizard就建立起工程文件,这样第一个被编译的文件就是stdafx.cpp。
◎当Visual C++编译stdafx.cpp文件时,它将结果保存在一个名为stdafx.pch的文件里。 (扩展名pch表
示预编译头文件。)
◎当Visual C++编译随后的每个.cpp文件时,它阅读并使用它刚生成的.pch文件。 Visual C++不再分析
Windows include文件,除非你又编缉了stdafx.cpp或stdafx.h。
这个技术很精巧,你不这么认为吗?(还要说一句,Microsoft并非是首先采用这种技术的公司,Borland
才是。) 在这个过程中你必须遵守以下规则:
◎你编写的任何.cpp文件都必须首先包含stdafx.h。
◎如果你有工程文件里的大多数.cpp文件需要.h文件,顺便将它们加在stdafx.h (后部)上,然后预编译
stdafx.cpp。
◎由于.pch文件具有大量的符号信息,它是你的工程文件里最大的文件。
如果你的磁盘空间有限,你就希望能将这个你从没使用过的工程文件中的.pch文件删除。执行程序时并不需
要它们,且随着工程文件的重新建立,它们也自动地重新建立。
----------------------------------------------------------------------------------------------
DLL中函数调用简单小例(原创)
下面通过例子介绍如何在动态链接库中定义函数、资源、和类以及如何在工程中使用动态链接库中已定义的
函数、资源、类。
Dll 的定制
步骤和方法:
第一步:
运行AppWizard,定义项目名为mydll,选择MFC AppWizard(dll),而不是MFC AppWizards(exe)。
第二步:
在这个例子中,只有一个AppWizard屏幕出现,选择MFC扩展
DLL(MFC Extension DLL (using shared MFC DLL),点击FINISH生成工程。
第三步:
点击File中的New,选择C/C++ Header File,File Name中输入mydll.h,点击OK,创建mydll.h。输入extern
"C" int PASCAL EXPORT sum(int i, int m, int n);,保存。
第四步:
在mydll.cpp中添加
#include "mydll.h"
extern "C" int PASCAL EXPORT sum(int i, int m, int n)
{
return i+m+n;
}
第五步:
在mydll.def 中添加
EXPORTS
Sum //标记出口
第六步:编译 生成mydll.lib and mydll.dll
主程序定制
第一步:
选择Project 中Add To Project 中的New , 重新生成一个工程,选择MFC AppWizards(exe),项目名为
textdll , 选择dialog ,点击FINISH,生成一个新的工程。。第二步:
拷贝…\mydll\debug\mydll.dll 到 ..\ textdll\debug\下,
拷贝…\mydll\debug\mydll.lib到…\textdll\目录下。
拷贝…\mydll\ mydll.h 到 ..\ textdll\下,
第三步:
把mydll.h添加到工程里,在textdll.cpp里加入
#include “mydll.h”
第四步:
添加edit控件IDC_EDIT1 和对应的m_edit1 (int)
第五步:
在button1中添加如下代码
m_edit1=sum(1,2,3);
UpdateData(false);
第六步:
在project -setting –link 添加mydll.lib
第七步:
可以编译了
注意:
1 。Mydll.lib and mydll.dll 的位置 要放对,并且和link对应
2.不要忘记在mydll.def 中添加下面的代码 ,至关重要 它是出口
EXPORTS
; Explicit exports can go here
Sum
3,它是正则dll,用来调用函数的,,
4,这是是静态调用方式,还有一种动态调用的方式
----------------------------------------------------------------------------------------------
一个lib和dll的例子 来自MSDN
环境:Visual studi
To create a new static library project
From the File menu, select New and then Project….
From the Project types pane, under Visual C++, select Win32.
From the Templates pane, select Win32 Console Application.
Choose a name for the project, such as MathFuncsLib, and enter it in the Name field. Choose a
name for the solution, such as StaticLibrary, and enter it in the Solution Name field.
Press OK to start the Win32 application wizard. From the Overview page of the Win32 Application
Wizard dialog, press Next.
From the Application Settings page of the Win32 Application Wizard, under Application type,
select Static library.
From the Application Settings page of the Win32 Application Wizard, under Additional options,
deselect Precompiled header.
Press Finish to create the project.
To add a class to the static library
To create a header file for a new class, from the Project menu, select Add New Item…. The Add
New Item dialog will be displayed. From the Categories pane, under Visual C++, select Code.
From the Templates pane, select Header File (.h). Choose a name for the header file, such as
MathFuncsLib.h, and press Add. A blank file will be displayed.
Add a simple class named MyMathFuncs to do common mathematical operations, such as addition,
subtraction, multiplication, and division. The code should resemble the following:
// MathFuncsLib.h
namespace MathFuncs
{
class MyMathFuncs
{ public:
// Returns a+ b
static double Add(double a, double b);
// Returns a - b
static double Subtract(double a, double b);
// Returns a * b
static double Multiply(double a, double b);
// Returns a / b
// Throws DivideByZeroException if b is 0 static double Divide(double a, double b);
};
}
To create a source file for a new class, from the Project menu, select Add New Item…. The Add
New Item dialog will be displayed. From the Categories pane, under Visual C++, select Code.
From the Templates pane, select C++ File (.cpp). Choose a name for the source file, such as
MathFuncsLib.cpp, and press Add. A blank file will be displayed.
Implement the functionality for MyMathFuncs in the source file. The code should resemble the
following:
// MathFuncsLib.cpp
// compile with: /c /EHsc
#include "MathFuncsLib.h"
#include
using namespace std;
namespace MathFuncs{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}
double MyMathFuncs::Subtract(double a, double b)
{
return a - b;
}
double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
} double MyMathFuncs::Divide(double a, double b)
{
if (b == 0)
{ throw new invalid_argument("b cannot be zero!");
}
return a / b;
}
}
To build the project into a static library, from the Project menu, select MathFuncsLib
Properties…. From the left pane, under Configuration Properties, select General. From the
right pane, change the Configuration Type to Static Library (.lib). Press OK to save the
changes.
Note
If building from the command line, you must build the program in two steps. First, compile the
code using Cl.exe with the /c compiler option (cl /c /EHsc MathFuncsLib.cpp). This will create
an object file named MathFuncsLib.obj. For more information, see /c (Compile Without Linking).
Second, link the code using the Library Manager Lib.exe (lib MathFuncsLib.obj). This will
create the static library MathFuncsLib.lib. For more information on the Library Manager, see
LIB Reference.
Compile the static library by selecting Build Solution from the Build menu. This creates a
static library that can be used by other programs.
To create an application that references the static library
To create an application that will reference and use the static library that was just created,
from the File menu, select New and then Project….
From the Project types pane, under Visual C++, select Win32.
From the Templates pane, select Win32 Console Application.
Choose a name for the project, such as MyExecRefsLib, and enter it in the Name field. Next to
Solution, select Add to Solution from the drop down list. This will add the new project to the
same solution as the static library.
Press OK to start the Win32 Application Wizard. From the Overview page of the Win32 Application
Wizard dialog, press Next.
From the Application Settings page of the Win32 Application Wizard, under Application type,
select Console application.
From the Application Settings page of the Win32 Application Wizard, under Additional options,
deselect Precompiled header.
Press Finish to create the project.
To use the functionality from the static library in the console application
After you create a new Console Application, an empty program is created for you. The name for
the source file will be the same as the name you chose for the project above. In this example,
it is named MyExecRefsLib.cpp.
To use the math routines that were created in the static library, you must reference it. To do
this, select References… from the Project menu. From the Property Pages dialog, expand the
Common Properties node and select References. Then select the Add New Reference… button. For
more information on the References… dialog, see References, Common Properties,
Property Pages Dialog Box.
The Add Reference dialog is displayed. This dialog lists all the libraries that you can
reference. The Project tab lists all the projects in the current solution and any libraries
they contain. From the Projects tab, select MathFuncsLib. Then select OK. For more information
on the Add Reference dialog, see Add Reference Dialog Box.
//指定头文件和lib文件的路径
To reference the header files of the static library, you must modify the include directories
path. To do this, from the Property Pages dialog, expand the Configuration Properties node,
then the C/C++ node, and select General. Next to Additional Include Directories, type in the
path to the location of the MathFuncsLib.h header file. //把头文件包括进来.
You can now use the MyMathFuncs class in this application. Replace the contents of
MyExecRefsLib.cpp with the following code:
Copy Code
// MyExecRefsLib.cpp
// compile with: /EHsc /link MathFuncsLib.lib
#include
#include "MathFuncsLib.h"
using namespace std;
int main()
{
double a = 7.4;
int b = 99;
cout << "a + b = " << MathFuncs::MyMathFuncs::Add(a, b) << endl;
cout << "a - b = " << MathFuncs::MyMathFuncs::Subtract(a, b) << endl;
cout << "a * b = " << MathFuncs::MyMathFuncs::Multiply(a, b) << endl;
cout << "a / b = " << MathFuncs::MyMathFuncs::Divide(a, b) << endl;
return 0;
}
Build the executable by selecting Build Solution from the Build menu.
To run the application
Make sure MyExecRefsLib is selected as the default project. From the Solution Explorer, select
MyExecRefsLib, and then select Set As StartUp Project from the Project menu.
To run the project, select Start Without Debugging from the Debug menu. The output should look
like this:
Copy Code
a + b = 106.4a - b = -91.6a * b = 732.6a / b = 0.0747475
//////////////////////////////////////////////////////////////////////////////////////////////
To create a new dynamic link library (DLL) project
From the File menu, select New and then Project….
From the Project types pane, under Visual C++, select Win32.
From the Templates pane, select Win32 Console Application.
Choose a name for the project, such as MathFuncsDll, and enter it in the Name field. Choose a
name for the solution, such as DynamicLibrary, and enter it in the Solution Name field.
Press OK to start the Win32 application wizard. From the Overview page of the Win32 Application
Wizard dialog, press Next.
From the Application Settings page of the Win32 Application Wizard, under Application type,
select DLL if it is available or Console application if DLL is not available. Some versions of
Visual Studio do not support creating a DLL project using wizards. You can change this later to
make your project compile into a DLL.
From the Application Settings page of the Win32 Application Wizard, under Additional options,
select Empty project.
Press Finish to create the project.
To add a class to the dynamic link library
To create a header file for a new class, from the Project menu, select Add New Item…. The Add
New Item dialog will be displayed. From the Categories pane, under Visual C++, select Code.
From the Templates pane, select Header File (.h). Choose a name for the header file, such as
MathFuncsDll.h, and press Add. A blank file will be displayed.
Add a simple class named MyMathFuncs to do common mathematical operations, such as addition,
subtraction, multiplication, and division. The code should resemble the following:
Copy Code
// MathFuncsDll.h
namespace MathFuncs
{
class MyMathFuncs
{
public:
// Returns a+ b
static __declspec(dllexport) double Add(double a, double b);
// Returns a - b static __declspec(dllexport) double Subtract(double a, double b);
// Returns a * b
static __declspec(dllexport) double Multiply(double a, double b);
// Returns a/ b
// Throws DivideByZeroException if b is 0
static __declspec(dllexport) double Divide(double a, double b);
};
}
Note the __declspec(dllexport) modifier in the method declarations above. These modifiers
enable the method to be exported by the DLL so they can be used by other applications. For more
information, see dllexport, dllimport.
To create a source file for a new class, from the Project menu, select Add New Item…. The Add
New Item dialog will be displayed. From the Categories pane, under Visual C++, select Code.
From the Templates pane, select C++ File (.cpp). Choose a name for the source file, such as
MathFuncsDll.cpp, and press Add. A blank file will be displayed.
Implement the functionality for MyMathFuncs in the source file. The code should resemble the
following:
Copy Code
// MathFuncsDll.cpp
// compile with: /EHsc /LD
#include "MathFuncsDll.h"
#include
using namespace std;
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{ return a + b;
}
double MyMathFuncs::Subtract(double a, double b)
{
return a -b;
}
double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
} double MyMathFuncs::Divide(double a, double b)
{
if (b == 0)
{ throw new invalid_argument("b cannot be zero!");
}
return a / b;
}
}
To build the project into a DLL, from the Project menu, select MathFuncsDll Properties…. From
the left pane, under Configuration Properties, select General. From the right pane, change the
Configuration Type to Dynamic Library (.dll). Press OK to save the changes.
Note
If building from the command line, use the /LD compiler option to specify that the output file
should be a DLL. For more information, see /MD, /MT, /LD (Use Run-Time Library).
Compile the dynamic link library by selecting Build Solution from the Build menu. This creates
a DLL that can be used by other programs. For more information on DLLs, see DLLs.
To create an application that references the dynamic link library
To create an application that will reference and use the dynamic link library that was just
created, from the File menu, select New and then Project….
From the Project types pane, under Visual C++, select Win32.
From the Templates pane, select Win32 Console Application.
Choose a name for the project, such as MyExecRefsDll, and enter it in the Name field. Next to
Solution, select Add to Solution from the drop down list. This will add the new project to the
same solution as the dynamic link library.
Press OK to start the Win32 Application Wizard. From the Overview page of the Win32 Application
Wizard dialog, press Next.
From the Application Settings page of the Win32 Application Wizard, under Application type,
select Console application.
From the Application Settings page of the Win32 Application Wizard, under Additional options,
deselect Precompiled header.
Press Finish to create the project.
To use the functionality from the class library in the console application
After you create a new Console Application, an empty program is created for you. The name for
the source file will be the same as the name you chose for the project above. In this example,
it is named MyExecRefsDll.cpp.
To use the math routines that were created in the dynamic link library, you must reference it.
To do this, select References… from the Project menu. From the Property Pages dialog, expand
the Common Properties node and select References. Then select the Add New Reference… button.
For more information on the References… dialog, see References, Common Properties,
Property Pages Dialog Box.
The Add Reference dialog is displayed. This dialog lists all the libraries that you can
reference. The Project tab lists all the projects in the current solution and any libraries
they contain. From the Projects tab, select MathFuncsDll. Then select OK. For more information
on the Add Reference dialog, see Add Reference Dialog Box.
To reference the header files of the dynamic link library, you must modify the include
directories path. To do this, from the Property Pages dialog, expand the Configuration
Properties node, then the C/C++ node, and select General. Next to Additional Include
Directories, type in the path to the location of the MathFuncsDll.h header file.
//用环境变量批量DLL的路径
Dynamic link libraries are not loaded by the executable until runtime. You must tell the system
where to locate MathFuncsDll.dll. This is done using the PATH environment variable. To do this,
from the Property Pages dialog, expand the Configuration Properties node and select Debugging.
Next to Environment, type in the following: PATH=
, where
to MathFuncsDll.dll file> is replaced with the actual location of MathFuncsDll.dll. Press OK to
save all the changes made.
Note
If you intend to run the executable from the command line rather than from within Visual
Studio, then you must manually update the PATH environment variable from the command prompt as
follows: set PATH=%PATH%;
, where
is replaced with the actual location of MathFuncsDll.dll.
You can now use the MyMathFuncs class in this application. Replace the contents of
MyExecRefsDll.cpp with the following code:
Copy Code
// MyExecRefsDll.cpp
// compile with: /EHsc /link MathFuncsDll.lib
#include #include "MathFuncsDll.h"
using namespace std;
int main()
{
double a = 7.4;
int b = 99;
cout << "a+ b = " <cout << "a - b = " <cout << "a * b = " <cout << "a / b = " <return 0;
}
Build the executable by selecting Build Solution from the Build menu.
To run the application
Make sure MyExecRefsDll is selected as the default project. From the Solution Explorer, select
MyExecRefsDll, and then select Set As StartUp Project from the Project menu.
To run the project, select Start Without Debugging from the Debug menu. The output should look
like this:
Copy Code
a + b = 106.4a - b = -91.6a * b = 732.6a / b = 0.0747475
-------------------------------------------------------------------------------------------
你好,看了你写的"VC++ DLL编程深入浅出",特别有收获。 只是有个地方我老搞不明白,就是用DLL导出全
局变量时,指定了.lib的路径(#pragma comment(lib,"dllTest.lib")),那么.dll的文件的路径呢,我尝
试着把.dll文件移到别的地方程序就无法正常运行了,请问.dll在这里怎么指定。
回答:
Windows按下列顺序搜索DLL:
(1)当前进程的可执行模块所在的目录;
(2)当前目录;
(3)Windows 系统目录,通过GetSystemDirectory 函数可获得此目录的路径;
(4)Windows 目录,通过GetWindowsDirectory 函数可获得此目录的路径;
(5)PATH 环境变量中列出的目录。
因此,隐式链接时,DLL文件的路径不需要指定也不能指定,系统指定按照1~5的步骤寻找DLL,但是对
应的.lib文件却需要指定路径;如果使用Windows API函数LoadLibrary动态加载DLL,则可以指定DLL的路径
。
在VC++中,如果生成DLL可以不使用.def文件。你只需要在VC++的函数定义前要加__declspec(dllexport)修
饰就可以了。但是使用__declspec(dllexport)和使用.def文件是有区别的。如果你的DLL是提供给VC++用户
使用的,你只需要把编译DLL时产生的.lib提供给用户,它可以很轻松地调用你的DLL。但是如果你的DLL是供
VB、PB、Delphi用户使用的,那么会产生一个小麻烦。因为VC++对于__declspec(dllexport)声明的函数会进
行名称转换,
----------------------------------------------------------------------------------------------
32 位编译器版本中,可以使用 __declspec(dllexport) 关键字从 DLL 导出数据、函数、类或类成员函数。
__declspec(dllexport) 将导出指令添加到对象文件
若要导出函数,__declspec(dllexport) 关键字必须出现在调用约定关键字的左边(如果指定了关键字)。
例如:
__declspec(dllexport) void __cdecl Function1(void);
若要导出类中的所有公共数据成员和成员函数,关键字必须出现在类名的左边,如下所示:
class __declspec(dllexport) CExampleExport : public CObject
{ ... class definition ... };
生成 DLL 时,通常创建一个包含正在导出的函数原型和/或类的头文件,并将 __declspec(dllexport) 添加
到头文件中的声明。若要提高代码的可读性,请为 __declspec(dllexport) 定义一个宏并对正在导出的每个
符号使用该宏:
#define DllExport __declspec( dllexport )
__declspec(dllexport) 将函数名存储在 DLL 的导出表中。如果希望优化表的大小
阅读(3048) | 评论(0) | 转发(1) |