[1] 首先创建库文件libhello.c
#include <stdio.h>
#include "libhello.h"
void hello()
{
printf("welcome to Linux!\n");
}
[2] 创建头文件libhello.h
void hello();
[3] 先将其编译成目标文件:
gcc -c libhello.c
[4] 现在我们创建libhello静态库文件:
 gcc -c libhello.c -o libhello.o
 ar rcs libhello.a libhello.o  
[5] 写一个测试程序test.c:
 #include <stdio.h>
int main(void)
{
    printf("test!\n");
    hello();   
}
[6] 编译与链接:
gcc -c test.c -o test.o
gcc test.o -L. -lhello -o test

 

 makefile

 test:test.o

gcc $^ -L. -lhello  -o $@
.c.o:
gcc -c $< 
run:
./test

 

 

相关文章:

  • 2021-07-05
  • 2022-02-14
  • 2021-05-21
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
  • 2021-09-18
  • 2021-07-04
猜你喜欢
  • 2021-06-26
  • 2021-05-28
  • 2021-10-29
  • 2022-01-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案