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;
}
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;
}
相关文章推荐
- IOS消息推送。
- 无法向会话状态服务器发出会话状态请求
- MySQL优化之BTree索引使用规则
- 图像处理
- [转]Linux内核源码详解--iostat
- Dijkstra(变形) POJ 1797 Heavy Transportation
- 使用Memcached提高.NET应用程序的性能
- 页面中如何让标点不出现在行首
- Handler, Loop, MessageQueue的工作原理
- 我们正处于一个绝佳的时期-工业4.0感受
- POJ1836 双向LIS
- Linux多线程与同步
- 基于OCS实现高速缓存
- savedInstanceState吐血详解
- LAMP一体环境快速安装
- Eclipse配置Tomcat服务器
- clientX,pageX,screenX,offsetLeft,scrollTop
- Photoshop投影与CSS中box-shadow的转换
- Android自助餐之SQLite
- MySQL的btree索引和hash索引的区别