1.生成so的代码由max.h和max.c组成.
//max.h
#ifndef _MAX_H
#define _MAX_H
int max(int a, int b);
#endif
//max.c
#include "max.h"
#include "stdio.h"
int max(int a, int b)
{
return (a > b ? a : b);
}
用下面这个命令生成so文件
gcc -shared max.c -o libmax.so; cp libmax.so /usr/lib
或者:gcc -shared max.c -o libmax.so
2.测试so的例子
//test_so.c
#include "stdio.h"
#include "max.h"
int main(int argc, char **argv)
{
printf("test so max=%d\n", max(1,2));
return 0;
}
用下面这个命令来生成可执行程序
gcc -lmax test_so.c -o test_so
或者:gcc -lm ./libmax.so test_so.c -o test_so
>>一个最简单的方式:你把编译命令写进Makefile就可以了
>>如果想找一个复杂的,直接看Linux下面的so库程序源代码就可以了
转载地址:
http://bbs.chinaunix.net/thread-2166323-1-1.html