您的位置:首页 > 其它

Windows 静态库

2015-11-27 17:06 417 查看
1 静态库的特点

1.1 目标程序的归档

1.2 静态库的代码会被嵌入到程序当中。

1.3 程序执行时不需要静态库存在

2 C语言静态库

2.1 创建静态库

创建Win32静态库项目,使用*.C文件建立

项目。

2.2 添加静态库函数

2.3 在程序中将静态库导入

2.3.1 项目的Setting里设置

2.3.2 使用关键字 pragma

#pragma comment(lib, "../lib/winclib.lib")

2.4 使用静态库提供的函数

在C语言程序中,直接使用函数即可。

3 C++语言的静态库

3.1 创建静态库

创建Win32静态库项目,使用*.CPP文件建立

项目。

3.2 添加静态库的函数

3.3 导入静态库

3.3.1 项目的Setting里设置

3.3.2 使用关键字 pragma

#pragma comment(lib, "../lib/wincpplib.lib")

3.4 定义库函数的原形

int CPP_Add( int nAdd1, int nAdd2 );

3.5 使用库函数

3.6 注意:

如果在CPP文件使用C语言静态库,定义的

静态库函数原形,需要增加 extern "C".

例如:

extern "C" int C_Add( int nAdd1, int nAdd2 );

//代码

// uselib.c : Defines the entry point for the console application.

//导入静态库

#pragma comment(lib, "../lib/winclib.lib")

int main( )

{

int nAdd = 0;

int nSub = 0;

//使用C静态库的函数

nAdd = C_Add( 100, 100 );

nSub = C_Sub( 100, 100 );

printf( "ADD: %d\n", nAdd );

printf( "SUB: %d\n", nSub );

return 0;

}

// usecpplib.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

//导入C++的静态库

#pragma comment( lib, "../lib/wincpplib.lib" )

#pragma comment( lib, "../lib/winclib.lib" )

//定义函数原形

int CPP_Add( int nAdd1, int nAdd2 );

int CPP_Sub( int nSub1, int nSub2 );

extern "C" {

int C_Add( int nAdd1, int nAdd2 );

int C_Sub( int nSub1, int nSub2 );

}

int main(int argc, char* argv[])

{ //使用C++库函数

int nAdd = CPP_Add( 100, 100 );

int nSub = CPP_Sub( 100, 100 );

printf( "ADD: %d\n", nAdd );

printf( "SUB: %d\n", nSub );

//使用C库函数

int nAdd2 = C_Add( 100, 100 );

int nSub2 = C_Sub( 100, 100 );

printf( "C_ADD: %d\n", nAdd );

printf( "C_SUB: %d\n", nSub );

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: