windows库

1、windows库的存在方式

1.1、静态库:不能被加载的程序,可以理解为目标程序的归档;*.lib。

1.2、动态库:是可以被加载的程序;*.dll。

2、静态库

2.1、静态库的特点
    目标程序的归档;
    静态库的代码会被嵌入到程序当中;
    程序执行时不需要静态库存在;
    致使程序很大。

2.2、C语言的静态库
2.2.1、创建静态库
    创建Win32静态库项目,使用*.c文件建立项目。
2.2.2、添加静态库函数
2.2.3、在程序中将静态库导入
    项目设置中可以添加lib文件;
    或者使用关键字pragma。
    #pragma comment(lib,"../Lib/Winclib.lib")
2.2.4、使用静态库提供的函数
    在C语言程序中,直接使用函数即可。

2.2.5、实例,(使用项目设置链接编译输出目录等,将库文件输出在../Lib/下,执行程序输出在../Bin/下)

2.2.5.1、vc++6.0创建一个静态库项目,添加源文件winclib.c,代码如下

1 int C_Add( int nAdd1, int nAdd2 ){
2     return ( nAdd1 + nAdd2 );
3 }
4 
5 int C_Sub( int nSub1, int nSub2 ){
6     return ( nSub1 - nSub2 );
7 }
winclib.c

编译后为winclib.lib

2.2.5.2、vc++6.0创建一个win32控制台应用程序,添加源文件useclib.c,代码如下

 1 //导入静态库;
 2 #pragma comment(lib,"../Lib/Winclib.lib")
 3 
 4 int main( )
 5 {
 6     int nAdd = 0;
 7     int nSub = 0;
 8     
 9     //使用静态库函数;
10     nAdd = C_Add( 100, 200 );
11     nSub = C_Sub( 100, 200 );
12 
13     printf( "ADD:%d
", nAdd );
14     printf( "SUB:%d
", nSub );
15 
16     return 0;
17 }
useclib.c

注意,编译时会出现警告,但是不影响编译和连接;

编译后为uscclib.exe

2.3、C++语言的静态库
2.3.1、创建静态库
    创建win32静态库项目,使用*.cpp文件建立项目。
2.3.2、添加静态库的函数
2.3.3、导入静态库
    #pragma comment( lib, "../Lib/Wincpplib.lib")
2.3.4、定义库函数的原型
    extern int CPP_Add(int nAdd1, int nAdd2 );
    extern int CPP_Sub(int nSub1, int nSub2 );
2.3.5、使用库函数

2.3.6、实例

2.3.6.1、vc++6.0创建一个静态库项目,添加源文件wincpplib.cpp,代码如下

1 int CPP_Add( int nAdd1, int nAdd2 ){
2     return ( nAdd1 + nAdd2 );
3 }
4 
5 int CPP_Sub( int nSub1, int nSub2 ){
6     return ( nSub1 - nSub2 );
7 }
wincpplib.cpp

2.3.6.2、vc++6.0创建一个win32控制台应用程序,添加源文件usecpplib.cpp,代码如下

 1 #include "stdafx.h"
 2 
 3 //导入C++的静态库;
 4 #pragma comment( lib, "../Lib/Wincpplib.lib")
 5 
 6 int main()
 7 {
 8     int nAdd = CPP_Add( 100, 200);
 9     int nSub = CPP_Sub( 100, 200);
10 
11     printf( "ADD:%d
", nAdd );
12     printf( "SUB:%d
", nSub );
13 
14     return 0;
15 }
usecpplib.cpp

2.3.7、在C++文件中使用C源文件生成的库

注意:此时,需要看C源文件使用的是C编译器还是C++编译器来生成的库,

  若是使用C编译器生成的库,需要额外加extern "C"代码;

  若是使用C++编译器生成的库,可不需要加这样的代码。

如下:
    extern "C" {
        int C_Add(int nAdd1, int nAdd2 );
        int C_Sub(int nSub1, int nSub2 );
    }

vc++6.0创建一个win32控制台应用程序,添加源文件usecpplib.cpp,代码如下

 1 #include "stdafx.h"
 2 
 3 //导入C++的静态库;
 4 #pragma comment( lib, "../Lib/Wincpplib.lib")
 5 
 6 //导入C的静态库;
 7 #pragma comment(lib,"../Lib/Winclib.lib")
 8 
 9 //定义函数原型;注意:extern可加可不加;
10 extern int CPP_Add(int nAdd1, int nAdd2 );
11 extern int CPP_Sub(int nSub1, int nSub2 );
12 //此时,extern必须使用;
13 extern "C" {
14     int C_Add(int nAdd1, int nAdd2 );
15     int C_Sub(int nSub1, int nSub2 );
16 }
17 
18 int main(int argc, char* argv[])
19 {
20     int nAdd = CPP_Add( 100, 200);
21     int nSub = CPP_Sub( 100, 200);
22 
23     printf( "ADD:%d
", nAdd );
24     printf( "SUB:%d
", nSub );
25 
26     int nAdd2 = C_Add( 100, 200 );
27     int nSub2 = C_Sub( 100, 200 );
28     
29     printf( "ADD:%d
", nAdd2 );
30     printf( "SUB:%d
", nSub2 );
31     return 0;
32 }
wincpplib.cpp

3、动态库
3.1、动态库的特点
    可以提供模块化的方式,方便协调开发。
    对源代码保护。
    减少可执行文件的大小
    提高代码的重用率。
3.2、动态库的基本使用方法
    动态库的创建
    加载动态库
    获取并使用库函数、变量或类
    释放动态库
3.3、函数的动态库
3.3.1、动态库的创建
    创建DLL的项目
    增加动态库的函数
    导出动态库函数:需要使用__declspec(dllexport)代码。
3.3.2、动态库的使用
3.3.2.1、隐式链接
    导入LIB
    定义函数原型
    使用函数
    注意:这些库是Dll动态库的映射表。
    大量的win32的API基本都是使用的隐式链接,如kernel32.lib、user32.lib、gdi32.lib

实例:

vc++6.0创建一个静态库项目,添加源文件DllFunc.cpp,代码如下

1 __declspec(dllexport) int Dll_Add( int nAdd1, int nAdd2 ){
2     return ( nAdd1 + nAdd2 );
3 }
4 
5 __declspec(dllexport) int Dll_Sub( int nSub1, int nSub2 ){
6     return ( nSub1 - nSub2 );
7 }
DllFunc.cpp

vc++6.0创建一个win32控制台应用程序,添加源文件CallDllFunc.cpp,代码如下

 1 #include "stdafx.h"
 2 
 3 //隐式导入DLL的Lib文件;
 4 #pragma comment( lib, "../Lib/dllfunc.lib")
 5 
 6 //定义函数原型;
 7 int Dll_Add( int nAdd1, int nAdd2 );
 8 int Dll_Sub( int nSun1, int nSub2 );
 9 
10 int main(int argc, char* argv[])
11 {
12     int nAdd = Dll_Add( 100, 200 );
13     int nSub = Dll_Sub( 100, 200 );
14     printf("Dll_Add:%d
", nAdd );
15     printf("Dll_Sub:%d
", nSub );
16     return 0;
17 }
CallDllFunc.cpp

3.3.2.2、显示链接

原文地址:https://www.cnblogs.com/kim0zh/p/3998754.html