linux 静态库使用

[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

 

 

posted @ 2012-07-11 15:28  ahuo  阅读(231)  评论(0编辑  收藏  举报