Duplicate of the following question: C function conflict
你好,
在我当前的项目中,我必须使用某种接口(interface)库。函数名称由该接口(interface)给出,该函数的作用是开发人员选择。据我所知,一个项目将使用此功能,并且在进行编译时,您选择lib及其功能。我想做的是通过包装另一个并在mein函数中调用它来同时使用现有的lib和我的lib:
otherlib:
int function1 (int a) {
// do something
}
int function1 (int a) {
//my code here
otherlib::function1(a);
}
namespace old {
#include "otherlib.h"
}
namespace new {
function1 (int a) {
::function1(a);
}
}
最佳答案
C中的命名空间使用库名称前缀来解决,例如:
libfoo-> foo_function1
libbar-> bar_function1
这些前缀是实际的 namespace 。所以如果你写libbar
int bar_function1(int a) {
function1(a);
}
h1=dlopen("libfoo.so")
foo_function1=dlsym(h1,"function1")
h2=dlopen("libbar.so")
bar_function1=dlsym(h2,"function1")