使用GCC将库与重复类名相链接 [英] Linking Libraries with Duplicate Class Names using GCC

查看:165
本文介绍了使用GCC将库与重复类名相链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使GCC在链接包含同名类的库时产生警告?例如

Is there a way for GCC to produce a warning while linking libraries that contain classes with the same name? For example

Port.h

class Port {
public:
  std::string me();
};

Port.cpp

#include "Port.h"
std::string Port::me() { return "Port"; }

FakePort.h

FakePort.h

class Port {
public:
  std::string me();
};

FakePort.cpp

#include "FakePort.h"
std::string Port::me() { return "FakePort"; }

main.cpp

#include "Port.h"

int main() {
  Port port;
  std::cout << "Hello world from " << port.me() << std::endl;
  return 0;
}

建筑

# g++ -c -o Port.o Port.cpp
# ar rc Port.a Port.o
# g++ -c -o FakePort.o FakePort.cpp
# ar rc FakePort.a FakePort.o
# g++ -c -o main.o main.cpp
# g++ main.o Port.a FakePort.a
# ./a.out
  Hello world from Port

>更改图书馆订单

# g++ main.o FakePort.a Port.a
# ./a.out
  Hello world from FakePort

根据此页


如果一个符号被定义在两个不同的库中,gcc将使用它找到的第一个,忽略第二个,除非第二个包含在由于某些其他原因被包含的目标文件中。

If a symbol is defined in two different libraries gcc will use the first one it finds and ignore the second one unless the second one is included in an object file which gets included for some other reason.

所以上述行为是有意义的。不幸的是,我继承了一个相当大的代码库,不使用命名空间(并且添加它们现在是不可行的),并在多个库中使用一些通用类名。我想在链接时自动检测重复的名称,以确保类的错误副本不是意外被实例化。像:

So the above behavior make sense. Unfortunately I am inheriting a sizable code base that doesn't make use of namespaces (and adding them isn't feasible right now) and uses some generic class names throughout multiple libraries. I would like to automatically detect duplicate names at link time to make sure that the wrong copy of a class isn't accidentally being instantiating. Something like:

# g++ -Wl,--warnLibraryDupSymbols main.o FakePort.a Port.a
  Warning: Duplicate symbol: Port

但我在GCC链接器选项中找不到任何东西。是否有可能让GCC自动检测并报告这种情况?

but I can't find anything in the GCC linker options to do that. Is it possible to get GCC to automatically detect and report such cases?

推荐答案

以下可能值得一试不知道它是否会做你想要的):

The following might be worth a try (I honestly don't know if it'll do what you want):


- 整个档案

对于在--whole-archive选项之后的命令行中提到的每个归档,将链接中的所有目标文件包含在归档中,归档所需的对象文件。这通常用于将归档文件转换为共享库,强制每个对象包含在生成的共享库中。此选项可以多次使用。

For each archive mentioned on the command line after the --whole-archive option, include every object file in the archive in the link, rather than searching the archive for the required object files. This is normally used to turn an archive file into a shared library, forcing every object to be included in the resulting shared library. This option may be used more than once.

我没有尝试过,但它听起来好像会拉入库中的所有项目,对象文件。您需要为每个库指定选项。

I haven't tried it, but it sounds as if it'll pull in all the items in a library as if they were object files. You'll need to specify the option for each library.

像Neil所说,这不会给你类级别的冲突,但如果有类成员与相同的签名,这可能使链接器告诉你。

As Neil said, this won't give you class-level conflicts, but if there are class members with the same signature, this might make the linker tell you.

这篇关于使用GCC将库与重复类名相链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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