
a.cpp :
#include "a.h"
#include
int xglupt()
{
std::cout < < "this is a haha" < < std::endl;
return 0;
}
b.cpp :
int xglupt()
{
std::cout < < "this is b hahaha" < < std::endl;
return 0;
}
然后我又创建了两个动态库,每个动态库连接一个静态库,每个动态库也只有一个函数,在这个函数里面会调用静态库里面的函数 代码分别如下:
c.cpp :
#include "a.h"
#include "c.h"
int cfunc()
{
return xglupt();
}
d.cpp :
#include "a.h"
#include "c.h"
int cfund()
{
return xglupt();
}
然后我又创建了一个可执行程序,这个可执行程序调用两个动态库的函数 代码如下:
e.cpp :
#include "d.h"
int main (int argc, char**argv)
{
cfunc();
dfunc();
return 0;
}
我期望的结果:
this is a haha
this is b hahaha
可实际结果根据连接时两个动态库的先后顺序分别是:
this is a haha
this is a haha
和:
this is b hahaha
this is b hahaha
就是说先加载的哪个动态库就会使用那个动态库链接的静态库中的函数。请问这个问题如何解决,如何才能得到我想要的结果? 谢谢大家了!
你的两瞎橡个so里做神戚面有相同的符号xglupt,当然是系统先加载哪个so就用那个so里面的xglupt的实现了。
解决的办法是要么改变接口的名字或者不改变名字但加上不同的参数(改变signature)让有所区分,要么加上class让他们有不同的纯陵name space