Linux下.a库文件的调用

来源:互联网 发布:python 文本分类 编辑:程序博客网 时间:2024/06/05 15:35

转自:http://blog.csdn.net/zengraoli/article/details/40741373


编译Linux使用的.a库文件

 

首先是需要编译成.a的源文件

 

hello.h

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #ifndef __INCLUDE_HELLO_H__  
  2. #define __INCLUDE_HELLO_H__  
  3.   
  4. void hello(const charchar *name);  
  5.   
  6.   
  7. #endif // end __INCLUDE_HELLO_H__  

hello.c

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include "stdio.h"  
  2.   
  3.   
  4. void hello(const charchar *name)  
  5. {  
  6.     printf("Hello world %s\n", name);  
  7. }  


和一个在linux平台上做测试的main.c

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include "hello.h"  
  2.   
  3.   
  4. int main()  
  5. {  
  6.     hello("everyone");  
  7.       
  8.     return 0;  
  9. }  


在Linux上面做测试,编译成.a文件,主要利用.o文件和ar命令


1、首先生成.o文件:

gcc -c hello.c

这样源代码的目录下就会产生一个hello.o



2、利用ar命令,从.o文件中创建.a文件

ar cr libhello.a hello.o

这样就可以生成.a文件了,注意,所要生成的.a文件的名字前三位最好是lib,否则在链接的时候,就可能导致找不到这个库



3、在linux下测试使用

编译main.c,并让hello.a链接到main中

gcc main.c -L. -lhello -o main(注意这里-L后面有个.)

这样在当面目录下面就出现了可执行程序main,直接运行就是我们索要的结果了



0 0
原创粉丝点击