我有一个可执行文件shared_main
、一个共享库libbar.so
和一个动态加载共享库libfoo.so
(通过dlopen
加载shared_main
).
shared_main
不使用libbar.so
中的任何符号,但libfoo.so
使用.
所以gcc -g -Wall -o shared_main shared_main.c libbar.so -ldl
不能把libbar.so
和shared_main
联系起来.
如何让gcc强制shared_main
链接libbar.so
?
P、 我知道我可以把libfoo.so
和libbar.so
联系起来.但我想try 一下,如果我能强迫shared_main
连接libbar.so
.
shared_main.c
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
int main(){
void* libHandle = dlopen("./libfoo.so", RTLD_LAZY);
if(libHandle == NULL){
printf("dlopen:%s", dlerror());
exit(1);
}
int(*xyz)(int);
xyz = (int(*)(int)) dlsym(libHandle, "xyz");
if(xyz == NULL){
printf("dlsym:%s", dlerror());
exit(1);
}
int b = xyz(3);
printf("xyz(3): %d\n", b);
}
傅.c(libfoo.so)
void func_in_bar();
int xyz(int b){
func_in_bar();
return b + 10;
}
wine 吧c(libbar.so)
//mimic python share library runtime
#include <stdio.h>
void func_in_bar(){
printf("I am a Function in bar\n");
}
void another_func_in_bar(){
printf("I am another function in bar\n");
}
生成文件
shared_main:
gcc -g -Wall -o shared_main shared_main.c libbar.so -ldl
shared:
gcc -g -Wall -fPIC -shared -o libfoo.so foo.c
gcc -g -Wall -fPIC -shared -o libbar.so bar.c