编译 库链接实例(动态库 隐式显式)

来源:互联网 发布:传奇数据库 编辑:程序博客网 时间:2024/06/14 08:42

代码结构:


main.c

#include <stdio.h>#include "help.h"int main(){  printf("%d\n", test(250));  return 0;}
main1.c

#include <stdio.h>#include <dlfcn.h>int main(){int ret = 1;void *pHandle;int (*pFunc)(int );pHandle = dlopen("./cj.so", RTLD_NOW);if (!pHandle){printf("Cann't find cj.so\n");return 1;}pFunc = (int (*) (int )) dlsym(pHandle, "test");if(pFunc){printf("result is %d\n",pFunc(250));}else{printf("Cann't find func test\n");}dlclose(pHandle);return 0;}
help.c

#include <stdio.h>#include "help.h"int test(int num){return num*2;}

help1.c

#include<stdio.h>#include "help.h"int test(int a){return a*3;}
help.h

#ifndef HELP_H#define HELP_Hint test(int );#endif


编译 链接:



使用 -I 指定main.c中使用到的头文件的路径(若和main.c在同一目录,可以不使用)

静态库的使用



-L 指定静态库路径


动态库 的 隐式链接:记得带路径链接动态库


无需再次链接,动态更换库


动态库的显示链接:

动态库流程


                                                




各方式对比:


原创粉丝点击