Window s DLL 编程 一、静态链接库 1
创建一个“Win32 控制台应用程序”项目
项目名定义为 staticdll
在“应用程序设置”中配置为“静态库”,其他默认
向生成的项目中添加一个头文件和一个实现文件,名称分别为staticlib
h 和staticlib
分别向这两个文中添加如下代码: //staticlib
h #ifndef LIB_H #define LIB_H extern "C" int add(int x,int y); //声明为C编译、连接方式的外部函数 #endif //staticlib
cpp #include "stdafx
h" #include "staticlib
h" int add(int x,int y) { return x + y; } 4
编译链接后会在解决方案的根目录下的debug 目录中得到一个staticdll
lib 库文件
该文件就是静态库的关键文件
使用该库文件,将其编译链接到用户程序中,就可以调用在静态库中定义的导出函数add
测试静态库staticdll
1)创建一个“w in32 控制台应用程序”项目
名称为:staticdlltest
2)配置“应用程序设置”为“控制台应用程序”,其他默认
3)在主 staticdlltest
cpp 文件中主要代码如下所示
// StaticDllTest
cpp : 定义控制台应用程序的入口点
// #include "stdafx
h" #include "
\StaticDll\staticLib
h" #pragma comment(lib, "
\\debug\\staticdll
lib") //指定与静态库一起连接 int _tmain(int argc, _TCHAR* argv[]) {