在 C++ 中使用多个静态库解决冲突 [英] Conflict resolution using multiple static libraries in C++

查看:484
本文介绍了在 C++ 中使用多个静态库解决冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

在文件中,vendor1.h.有以下声明:

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

double Min();

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

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

double Min();

在我的客户端文件中:

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

它默认调用 vendor1.h.我尝试引入命名空间:

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

   namespace vendor1 {  
    include "vendor1.h"
   }

   namespace vendor2 {  
    include "vendor2.h"
   }

调用以下函数

double xx = vendor2::Min();

我收到以下链接器错误:

I get the following linker errors:

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

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?

推荐答案

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

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.

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

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.

这篇关于在 C++ 中使用多个静态库解决冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆