Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用导出相同函数名的2个C库_C++_C_Word Wrap_Ambiguity - Fatal编程技术网

C++ 如何使用导出相同函数名的2个C库

C++ 如何使用导出相同函数名的2个C库,c++,c,word-wrap,ambiguity,C++,C,Word Wrap,Ambiguity,以下问题的副本: 嗨, 在我当前的项目中,我必须使用某种接口库。函数名由这个接口给出,这个函数所做的是开发人员的选择。据我所知,一个项目应该使用这个函数,当涉及到编译时,你可以选择lib和它的功能。我试图通过包装另一个库并在mein函数中调用它,同时使用现有库和我的库: 其他库: int function1 (int a) { // do something } mylib: int function1 (int a) { //my code here otherlib::funct

以下问题的副本:


嗨, 在我当前的项目中,我必须使用某种接口库。函数名由这个接口给出,这个函数所做的是开发人员的选择。据我所知,一个项目应该使用这个函数,当涉及到编译时,你可以选择lib和它的功能。我试图通过包装另一个库并在mein函数中调用它,同时使用现有库和我的库:

其他库:

int function1 (int a) {
// do something
}
mylib:

int function1 (int a) {
//my code here
    otherlib::function1(a);
}
问题是我没有访问另一个库的权限,而另一个库没有任何名称空间。我已经试过了

namespace old {
    #include "otherlib.h"
}
然后在我的函数中通过old::function1调用旧函数。只要它只是头文件,它就可以工作。库将其符号导出回全局空间。也像

namespace new {
    function1 (int a) {
        ::function1(a);
    }
}
没用。最后但并非最不重要的是,我尝试了ifdef和defines

但我没有成功

有什么办法解决这个问题吗?提前谢谢

编辑:我既不能访问旧库,也不能访问两个库都应用于的项目


EDIT2:至少旧库是一个静态库

使用库名前缀(如:

libfoo-->foo_函数1
libbar-->条形图函数1

这些前缀是实际的名称空间。所以如果你写libbar

int bar_function1(int a) {
     function1(a);
}
这是解决问题的方法

C有名称空间——它们只是称为前缀;)

另一个选项是通过动态加载库执行各种肮脏的操作,如:

h1=dlopen("libfoo.so")
foo_function1=dlsym(h1,"function1")

h2=dlopen("libbar.so")
bar_function1=dlsym(h2,"function1")

好像另一个库是C,你的代码是C++。您可能会遇到损坏问题(C++编译器会损坏符号——在符号名称中添加额外的内容以区分重载等)

如果库是纯C,您可以尝试:

extern "C" { // disable mangling of symbol names in the block
#include "otherlib.h"
}

namespace new_lib { // new is a reserved word
   int function1( int a ) {
      ::function1(a);
   }
}
我没有试过。还考虑提供错误信息。

另一个选项是(如果库是动态的)动态加载库并调用函数。在linux(我不知道windows)中,您可以使用dlopen打开库,使用dlsym获取符号并调用它:

// off the top of my head, not tried:
int function1( int a )
{
   int (*f)(int); // define the function pointer
   void * handle = dlopen( "library.so" );
   f = dlsym( handle, "function1" );
   f( a ); // calls function1(a) in the dynamic library
}
在这种情况下,由于您没有针对库进行链接,因此不会出现符号冲突,但同样,它仅对动态库有效,并且对于常规使用来说相当麻烦

更新

如果你的用户不会直接使用“OffLIB”(他们不包括他们的头),他们将只是C++,那么第一种方法是可能的(即使读起来很恐怖):


如果用户代码同时包含您的头和otherlib.h,那么它必须限定要调用哪个函数。

如果您真的很绝望,您可以编写一个使用名称空间或前缀或允许dlsym技巧的包装器库。此包装器库需要动态链接(以避免符号冲突)。然后,动态库可以安全地将旧的静态库嵌入其中。创建动态包装器库时,请确保不从静态库导出符号。

您无法在链接时解决此问题,因此需要在运行时通过动态库解决此问题。这些函数的符号基本上是在库生成后烘焙的。如果两个库输出相同的符号,则它们不能静态链接。

谢谢您的回答。不幸的是,我既不能访问旧库,也不能访问它将用于的项目。至少旧库是静态的。我看到了这一点,但我无法访问它们将用于的项目。感谢您的回复。你的第一个建议对我不起作用。我无法控制函数名,因此必须通过“使用命名空间new_lib;”更改作用域,这将把新的库放入全局函数中,函数在全局函数中已经定义。我试试第二种方法。
// newlib.h
namespace hideout {
   int f( int a );
}
using namespace hideout; // usually I would not put this on the header

// newlib.cpp
extern "C" { // if otherlib is C, else remove this line
#include "otherlib.h"
}
namespace hideout {
   int f( int a ) { return ::f( a*2 ); }
}

// main.cpp
#include "newlib.h"
int main()
{
   std::cout << f( 5 ) << std::endl;
}
// g++ 4.0 in macosx:
00002dbe T __ZN7hideout9function1Ei // namespace hideout, function1 takes int, returns int
00002db0 T _function1