C/C++库文件

目录

一、Windows环境

1.1 静态库

1.1.1 创建

1.1.2 使用

1.2 动态库

1.2.1 创建

1.2.2 使用

二、Linux环境

2.1 静态库

2.1.1 创建

2.1.2 使用

2.2 动态库

2.2.1 创建

2.2.2 使用


一、Windows环境

系统类型:Windows 7 旗舰版,64 位操作系统。

编译环境:VS2013 x64

 

1.1 静态库

1.1.1 创建

新建一个文件夹  win_lib_test\  ,然后在  win_lib_test\  下新建四份测试代码addition.c、addition.h,subtraction.c、subtraction.h,代码内容如下所示:


/* addition.h */

#ifndef _Included_Addition
#define _Included_Addition

#ifdef __cplusplus
extern "C" {
#endif

int add(int a, int b);

#ifdef __cplusplus
}
#endif

#endif
/* addition.c */

#include "addition.h"

int add(int a, int b)
{
    return a + b;
}

/* subtraction.h */

#ifndef _Included_Subtraction
#define _Included_Subtraction

#ifdef __cplusplus
extern "C" {
#endif

int sub(int a, int b);

#ifdef __cplusplus
}
#endif

#endif
/* subtraction.c */

#include "subtraction.h"

int sub(int a, int b)
{
    return a - b;
}

然后根据源文件生成静态库文件operator.lib,具体步骤如下:

利用   cl /c *.c  命令对源文件进行编译生成目标文件。

利用   lib *.obj /OUT:name.lib  命令对目标文件进行链接生成静态库文件。

到现在,我们就根据addition.c、subtraction.c这两个源文件生成了静态库文件operator.lib,接下来就是在Windows环境下如何使用静态库文件啦。

 

1.1.2 使用

在当前目录  win_lib_test\  下新建一个文件夹  main\ ,然后在  main\  下新建一份测试代码main.c,代码内容如下所示:


/* main.c */

#include <stdio.h>
#include "addition.h"
#include "subtraction.h"

#pragma comment(lib, "operator.lib")

int m
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值