#main.c
/*main.c*/
#include <stdio.h>
#include <dlfcn.h>
#define LIB "../libs/x86_64/libadd.so"
int main(void)
{
void *dl;
char *error;
int (*add)(int,int);
dl = dlopen(LIB,RTLD_LAZY);
add = dlsym(dl, "add");
printf("2+3=%d\n",add(2,3));
dlclose(dl); //close shared lib
return 0;
}
gcc -ldl -o test main.c
./test 提示 Segmentation fault
如何解决
libadd.so的代码来自
#add.h
/*add.h*/
#ifndef _ADD_H_
#define _ADD_H_
int add(int, int);
#endif
#add.c
/*add.c*/
#include "add.h"
int add(int x,int y)
{
return x+y;
}
刚开始学C 求大神指定
[root@localhost hello]# ls
add.c add.h Android.mk libadd.so main.c main.o
应该检查 dlopen 和 dlsym 的返回值.
可能是执行 test 时, 无法找到 libadd.so, 应该放在能被搜索到的路径.