Windows 静态库



  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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值