【问题标题】:C++: Avoiding duplicate symbol linker errorC++:避免​​重复符号链接器错误
【发布时间】:2014-04-29 13:56:40
【问题描述】:

我收到链接器错误:

duplicate symbol __ZN5ENDF64FileILNS_7MF_enumE1EE4readEv in:
    Read.cpp.o
    Material.cpp.o

重复的符号名称在哪里:

$ c++filt __ZN5ENDF64FileILNS_7MF_enumE1EE4readEv
  ENDF6::File<(ENDF6::MF_enum)1>::read()

我知道我不能在多个地方定义同一个函数——这就是导致此链接器错误的原因。 (我见过这个问题:ld: duplicate symbol)我认为我没有在多个地方定义read() 函数,但链接器(clang++)说我有。

我在哪里复制 read() 符号?

我的代码结构如下:

//MFs.hpp
#ifndef MFS_HPP
#define MFS_HPP
enum class MF_enum {
...
}
#endif


//File.hpp
#ifndef FILE_HPP
#define FILE_HPP

#include "MFs.hpp"

// Definition of class File
template<>
class File {
...
}

// Definition of File<...>::read() function
template <>
void File<1>::read()
{
    std::cout << "Reading into MF=1"<< std::endl;
}

#endif

没有File.cpp,因为File 类是模板化的。所有定义(和声明)都在File.hpp

// Material.cpp
#include "File.hpp"
...

// Material.hpp
#ifndef MATERIAL_HPP
#define MATERIAL_HPP

#include "File.hpp"
...
#endif

最后是驱动代码:

// Read.cpp
#include "Material.hpp"
#include "File.hpp"

int main (){
...
}

【问题讨论】:

  • 你是专攻File&lt;1&gt;::read()吗?如果是这样,你需要标记专业化inline(完整的专业化不再是模板,而是一个函数。[read函数的实际定义是这个问题的核心,它不应该隐藏在评论中:// Definition of File&lt;...&gt;::read()!!!]
  • 您是否在 MFs.hpp 中包含 #include "MFs.hpp" ?
  • @Ram:如果是这样,并且包含防护不起作用,它会触发编译器,而不是链接器,错误。
  • @DavidRodríguez-dribeas 如果他有一个包含保护,为什么它仍然会导致重复符号?
  • @0x499602D2:不同的翻译单元加载相同的标题,每个翻译单元只会看到一次定义,但会生成符号。然后链接器将失败,因为符号是在多个翻译单元中定义的。包含保护防止在单个翻译单元中看到相同的标题两次,而不是从定义相同符号的不同翻译单元中看到。

标签: c++ linker clang


【解决方案1】:

(完整)模板的特化不是模板本身。如果您正在专门研究该功能,那么您只需在标头中声明它并在单个翻译单元中提供实现,或者使定义内联:

// Header [1]
template <int>
class File {
   // ...
   void open();
};
template <>
void File<1>::open(); // just declaration

// Single .cpp
template <>
void File<1>::open() { ... }

或者:

// Header [2]
template <int>
class File {
   // ...
   void open();
};
template <>
inline void File<1>::open() { ... }

【讨论】:

    猜你喜欢
    • 2011-02-26
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 2013-06-09
    相关资源
    最近更新 更多