类 的名称冲突未在静态库接口中公开 (c++)

Name collision of classes not exposed in static libraries interface (c++)

我有两个静态库(a.libb.lib) 在内部,库 a 有 class Foo,但它没有暴露在任何公开可用的包含中。库 b.lib 也有 class Foo 并且它也没有暴露在界面中。这些 class 位于相同的命名空间并具有相同的构造函数签名。

当我调试或 运行 可执行文件(使用两个库)时,我发现创建了来自 lib a 而不是 lib b 的错误 class。 试图了解这是怎么发生的。是链接器问题吗? (即 class 名称相同,链接器首先插入它找到的任何内容)

静态库只是目标文件的存档。与静态库链接时,它与与单独的目标文件链接相同。目标文件基本上是一个 translation unit. And because of the One Definition Rule you can't have two different classes in different translation units with the same name, as that will lead to undefined behavior.

如果你想在库中包含 "private" 类,请使用 namespaces 并在其中定义 "private" 类 (唯一命名的)命名空间。