lib库的生成与使用

来源:互联网 发布:战略思维 知乎 编辑:程序博客网 时间:2024/05/22 15:21

 例:

test.h:

#include <stdlib.h>

#include <stdio.h>

#include <dlfcn.h>

int Max(int a, int b);

int display();

 

Max.c:

#include "test.h"

int Max(int a, int b)

{

return a > b ? a: b ;

}

 

display.c :

#include "test.h"

void display()

{

printf("Hello world\n");

}

 

test.c :

#include "test.h"

int main()

{

int ret = 0;

ret = Max(1, 10);

printf("Max is %d\n", ret);

display();

return 0;

}

 

1,把didplay.c, Max.c加入同时并生成libdis.so动态库

gcc display.c Max.c -fPIC -shared -o libdis.so

2,把libdis.so复制到系统/usr/lib文件夹中

cp libdis.so /usr/lib

3,把test.c与libdis.so绑定关系

gcc test.c -L. -ldis -o test

4,测试test是否绑定成功

ldd test

显示出地址即OK

5,运行test

./test

显示出执行函数的结果