Linux下建立静态库的实例讲解
2009-12-09 10:30
453 查看
关于[/b]Linux[/b]下建立一个静态库的简要步骤:[/b][/b]
静态库,也称作归档文件,通常他们的文件名以“.a”结尾。
下面我们来创建一个小型的函数库,包含两个函数。
第一,创建函数源文件,如fred.c和bill.c:
/*************fred.c***************/
#include <stdio.h>
void fred(int arg)
{
printf(“fred: you passed %d\n”, arg);
}
/*************bill.c***************/
#include <stdio.h>
void bill(char *arg)
{
printf(“bill: you passed %s\n”, arg);
}
第二,分别编译两个函数,产生要包含在库文件中的目标文件,即获得fred.o和bill.o:
gcc -c bill.c fred.c
第三,创建库文件lfoo.a:
ar crv lfoo.a bill.o fred.o
第四,为库文件创建一个头文件lib.h,为调用程序做准备:
/* This is a lib.h.*/
void fred(int arg);
void bill(char *);
第五,编写调用程序test.c:
#inlcude <lib.h>
int main()
{
bill(“hello world!”);
exit(0);
}
第六,编译运行:
(1)生成目标文件test.o:cgcc -c test.c;
(2)生成可执行文件test:gcc -o test test.o lfoo.a;
(3)运行test:./test
上述第六步,也可通过-L来连接静态库:
gcc -o test test.o -L -lfoo本文出自 “飞雪待剑” 博客,请务必保留此出处http://jazka.blog.51cto.com/809003/240509
静态库,也称作归档文件,通常他们的文件名以“.a”结尾。
下面我们来创建一个小型的函数库,包含两个函数。
第一,创建函数源文件,如fred.c和bill.c:
/*************fred.c***************/
#include <stdio.h>
void fred(int arg)
{
printf(“fred: you passed %d\n”, arg);
}
/*************bill.c***************/
#include <stdio.h>
void bill(char *arg)
{
printf(“bill: you passed %s\n”, arg);
}
第二,分别编译两个函数,产生要包含在库文件中的目标文件,即获得fred.o和bill.o:
gcc -c bill.c fred.c
第三,创建库文件lfoo.a:
ar crv lfoo.a bill.o fred.o
第四,为库文件创建一个头文件lib.h,为调用程序做准备:
/* This is a lib.h.*/
void fred(int arg);
void bill(char *);
第五,编写调用程序test.c:
#inlcude <lib.h>
int main()
{
bill(“hello world!”);
exit(0);
}
第六,编译运行:
(1)生成目标文件test.o:cgcc -c test.c;
(2)生成可执行文件test:gcc -o test test.o lfoo.a;
(3)运行test:./test
上述第六步,也可通过-L来连接静态库:
gcc -o test test.o -L -lfoo本文出自 “飞雪待剑” 博客,请务必保留此出处http://jazka.blog.51cto.com/809003/240509
相关文章推荐
- Linux---一些实例来讲解awk命令
- Linux实战案例(2)实例讲解使用软连接的场景和过程
- linux中fork()函数详解(原创!!实例讲解)
- 简单实例讲解linux的module模块编译步骤
- linux中fork()函数详解(原创!!实例讲解)
- linux学习——动态库和静态库实例分析
- 嵌入式Linux之我行——S3C2440上LCD驱动(FrameBuffer)实例开发讲解(一)
- linux中fork()函数详解(原创!!实例讲解) (转载)
- linux学习 建立静态库,动态库,写简单的makefile
- 【Linux编程】C/C++获取目录下文件或目录及linux中fork()函数详解(原创!!实例讲解)
- 实例讲解Linux系统中硬链接与软链接的创建
- 建立最简单的repo服务器实例讲解 推荐
- linux中fork()函数详解(原创!!实例讲解)
- Linux下sudo命令实例讲解
- 嵌入式Linux之我行——S3C2440上LCD驱动(FrameBuffer)实例开发讲解(一)
- linux中fork()函数详解(原创!!实例讲解)
- linux中fork()函数详解(原创!!实例讲解)
- linux中fork()函数详解(原创!!实例讲解)
- 嵌入式Linux之我行——S3C2440上LCD驱动(FrameBuffer)实例开发讲解(二)
- 嵌入式Linux之我行——S3C2440上MMC/SD卡驱动实例开发讲解(一)