linux静态库

来源:互联网 发布:双色球软件2016破解 编辑:程序博客网 时间:2024/06/06 05:52
静态库 1、创建静态库(1)写个静态库函数代码 //static_lib.c int add(int a,int b) { return a + b; } int sub(int a,int b) { return a - b; } int mul(int a,int b) { return a * b; } int div(int a,int b) { return a/b; } (2)编译该源文件 $gcc -c static_lib.c (3)使用ar工具创建一个静态库 ar rcs 静态库名 目标文件1 目标文件2...目标文件n 2、使用静态库(1)创建一个文件,声明静态库中全局变量和函数的声明 //static_lib.h extern int add(int a,int b); extern int sub(int a,int b); extern int mul(int a,int b); extern int div(int a,int b); (2)写一个主函数,使用库文件中的函数 #include #include "static_lib.h" int main(void) { int a,b; printf("please input a and b\n"); scanf("%d%d",&a,&b); printf("the add : %d", add(a,b)); printf("the sub : %d", sub(a,b)); printf("the mul : %d", mul(a,b)); printf("the div : %d", div(a,b)); return 0; } (3)生成可执行文件,使用-static连接静态库 $gcc main.c -static ./static_lib.a -o app
0 0
原创粉丝点击