一、生成静态库
新建一个hello.c,内容如下:
#include <stdio.h>
int hello()
{
printf("hello world\n") ;
return 0 ;
}
以及其头文件hello.h
#ifndef _HELLO_H
#define _HELLO_H
int hello() ;
#endif
用于测试的主函数文件test.c
#include "hello.h"
int main()
{
hello();
return 0 ;
}
编译源文件hello.c到目标文件hello.o
gcc -c hello.c -o hello.o
libtool -static -o libhello.a hello.o --MAC OS
ar crsv libhello.a testhello --linux
将库与源文件链接成可执行文件 testhello
gcc test.c libhello.a -o testhello
最后测试一下