编译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



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



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


gcc -c hello.c

这样源码的文件夹下就会产生一个hello.o

编译Linux使用的.a库文件


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

ar cr libhello.a hello.o

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

编译Linux使用的.a库文件


3、在linux下測试使用

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

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

这样在当面文件夹以下就出现了可执行程序main。直接执行就是我们索要的结果了

编译Linux使用的.a库文件

编译Linux使用的.a库文件

相关文章:

  • 2021-06-19
  • 2021-08-14
  • 2022-12-23
  • 2021-12-26
  • 2021-08-16
  • 2022-12-23
  • 2021-08-14
  • 2021-08-29
猜你喜欢
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2022-02-10
  • 2022-12-23
相关资源
相似解决方案