C++:避免重复的符号链接器错误
C++: Avoiding duplicate symbol linker error
我收到一个链接器错误:
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:重复符号)我不认为我在多个地方定义了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 (){
...
}
(完整)模板的专门化不是模板本身。如果您正在专门化函数,那么您需要在头中声明它并在单个翻译单元中提供实现,或者使定义内联:
// 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() { ... }
相关文章:
- 体系结构x86_64的未定义符号:链接器错误
- 可能的 GCC 链接器错误会导致将弱符号和局部符号链接在一起时出错
- 共享库中非模板基的模板子类导致未定义的符号类型信息'class'链接错误
- 链接错误:符号重复
- Glew - 与 GLFW 配对时 gl- 函数符号上出现链接错误
- Linux 共享库链接错误(未定义的符号)
- 尝试使用 std::函数(未定义的符号)声明模板化类状态时出现链接错误
- 标准::字符串::空的未定义符号错误;Mac OS High Sierra 上的 c++ 标准方法链接错误
- 单一实例对象的未解决外部符号链接错误
- 链接错误:找不到__thiscall函数符号,但定义了__cdecl函数符号
- 导出类静态常量的未解决外部符号链接错误(仅限某些项目)
- OpenCV 模块作为符号链接会导致 cmake 错误
- G 自定义动态库链接错误未定义符号
- DELAYLOAD在Qt LNK2001中给出链接错误:未解析的外部符号
- 建筑x86_64的未定义符号:链接错误
- 将类从应用程序解决方案移动到静态库解决方案会产生无法解决的外部符号链接错误
- Visual Studio 2015 C++ 未解析的外部符号链接错误
- 设置为 'default' 时"compile as"外部符号链接错误
- 为什么这种特殊情况会导致符号链接错误
- 如何解决动态链接XCode项目到lib4cxx库时的单符号链接错误