
正文
编译Linux使用的.a库文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
编译Linux使用的.a库文件
首先是须要编译成.a的源文件
hello.h:
#ifndef __INCLUDE_HELLO_H__
#define __INCLUDE_HELLO_H__void hello(const char *name);#endif // end __INCLUDE_HELLO_H__
hello.c
#include "stdio.h"void hello(const char *name)
{
printf("Hello world %s\n", name);
}
和一个在linux平台上做測试的main.c
#include "hello.h"int main()
{
hello("everyone"); return 0;
}
在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。直接执行就是我们索要的结果了
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemVuZ3Jhb2xp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">







