转载 from:http://blog.csdn.net/cnlm2/article/details/6100922
编译过程详解:http://www.cnblogs.com/laojie4321/archive/2012/03/28/2421021.html
print.h
#ifndef __PRINT_H__
#define __PRINT_H__
void print(const char* ptr);
#endif
print.c
#include "print.h"
#include <stdio.h>
void print(const char* ptr)
{
printf("%s", ptr);
}
testprint.c
#include "print.h"
int main()
{
print("hello,world!/n");
return 0;
}
文件vim编辑完成后:
1.编译动态库
gcc print.c -shared -fPIC -o libprint.so.1.2
编译测试
gcc testprint.c libprint.so.1.2
2.编译静态库
gcc -Wall -c print.c
ar -cvq libprint.a print.o
编译测试
gcc testprint.c -L. -lprint
gcc testprint.c -L./ -lprint
这里的-L接的.或者./表示链接库的路径