在 C++ 中使用多个静态库解决冲突;

发布于 2025-01-02 23:55:54 字数 981 浏览 1 评论 0原文

我有来自 «vendor1» 和 «vendor2» 的 2 个静态库:

  • vendor1.libvendor1.h
  • vendor2.libvendor2.h

在文件 vendor1.h 中。存在以下声明:

double Min();

在文件 vendor2.h 中。有以下声明:

double Min();

在我的客户端文件中:

include "vendor1.h"  
include "vendor2.h"  
double x = Min();

默认情况下调用 vendor1.h。我尝试引入命名空间:

   namespace vendor1 {  
    include "vendor1.h"
   }

   namespace vendor2 {  
    include "vendor2.h"
   }

调用以下函数时

double xx = vendor2::Min();

出现以下链接器错误:

Client.cpp 1>Client.obj:错误 LNK2019:引用了无法解析的外部符号“double __cdeclvendor2::Min(void)”(?Min@vendor2@@YANXZ) 在函数 _wmain 1>c:\temp\Client\Debug\Client.exe 中:致命错误 LNK1120:1 个未解析的外部

如何在不为每个包装器创建包装器的情况下解决此问题?

I have 2 static libraries from «vendor1» and «vendor2»:

  • vendor1.lib and vendor1.h;
  • vendor2.lib and vendor2.h.

In the file, vendor1.h. The following declaration is there:

double Min();

In the file, vendor2.h. The following declaration is there:

double Min();

In my client file:

include "vendor1.h"  
include "vendor2.h"  
double x = Min();

It by defaults calls vendor1.h. I tried introducing the namespace:

   namespace vendor1 {  
    include "vendor1.h"
   }

   namespace vendor2 {  
    include "vendor2.h"
   }

A call to the following function

double xx = vendor2::Min();

I get the following linker errors:

Client.cpp 1>Client.obj : error LNK2019: unresolved external symbol "double __cdecl vendor2::Min(void)" (?Min@vendor2@@YANXZ) referenced
in function _wmain 1>c:\temp\Client\Debug\Client.exe : fatal error
LNK1120: 1 unresolved externals

How can I fix this without creating wrappers for each of the wrappers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚笙 2025-01-09 23:55:54

如果您有两个名称冲突的静态库,您将无法静态链接您的程序!静态链接器将查找第一个与未定义符号匹配的符号并选择它。将名称包装到命名空间中没有帮助:这会更改库中预期的命名空间。您刚刚发现了为什么命名空间是一件好事。

如何解决问题?我不知道基于 C++ 标准的方法。实际上,您也许可以做一些事情:创建一个动态库,该动态库转发到冲突的函数,但将名称放入单独的名称空间(或使用不同的名称)。动态库与单独的静态库链接,即此时不会发生冲突。您可能还需要避免从共享库中的符号中看到基础名称。如何完成此操作的详细信息取决于编译器,我不知道该由谁来处理此类问题的 MSVC++。

If you have two static libraries with conflicting names you won't be able to link your program statically! The static linker will just go and find the first symbol matching an undefined symbol and choose this. Wrapping the names into a namespace doesn't help: this changes the namespace being expected in the library. You just found why namespace are a good thing.

How to solve the problem? I'm not aware of an approach which is based on the C++ standard. Practically, you may be able to do something: create a dynamic library which forwards to your conflicting functions but puts the name into separate namespaces (or uses different names). The dynamic libraries are linked with individual static libraries, i.e. the conflict doesn't happen at this time. You will probably also need to avoid the underlying names being visible from the symbols in the shared library. The details on how this is done depend on the compiler and I don't know who to deal with MSVC++ for things like this.

往日 2025-01-09 23:55:54

将函数包装到不同的命名空间中怎么样?

vendor1_wrapper.h:

namespace vendor1 {
    double Min();
}

vendor1_wrapper.cpp:

#include "vendor1_wrapper.h"
#include "vendor1.h"

namespace vendor1 {
    double Min()
    {
        return Min();
    }
}

vendor2_wrapper.h:

namespace vendor2 {
    double Min();
}

vendor2_wrapper.cpp:

#include "vendor2_wrapper.h"
#include "vendor2.h"

namespace vendor2 {
    double Min()
    {
        return Min();
    }
}

现在您可以使用这些函数使用命名空间(您的客户端文件):

#include "vendor1_wrapper.h"
#include "vendor2_wrapper.h"

...

vendor1::Min();
vendor2::Min();

How about wrapping the functions into different namespaces?

vendor1_wrapper.h:

namespace vendor1 {
    double Min();
}

vendor1_wrapper.cpp:

#include "vendor1_wrapper.h"
#include "vendor1.h"

namespace vendor1 {
    double Min()
    {
        return Min();
    }
}

vendor2_wrapper.h:

namespace vendor2 {
    double Min();
}

vendor2_wrapper.cpp:

#include "vendor2_wrapper.h"
#include "vendor2.h"

namespace vendor2 {
    double Min()
    {
        return Min();
    }
}

Now you can use the functions using namespaces (your client file):

#include "vendor1_wrapper.h"
#include "vendor2_wrapper.h"

...

vendor1::Min();
vendor2::Min();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文