C++: Avoiding duplicate symbol linker error

2019-05-11 00:56发布

问题:

I'm getting a linker error:

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

where the duplicate symbol name is:

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

I know that I can't define the same function in multiple places—that's what can cause this linker error. (I've seen this question: ld: duplicate symbol) I don't think I have the read() function defined in multiple places, but the linker (clang++) says I do.

Where am I duplicating the read() symbol?

My code structure looks like this:

//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

There is no File.cpp because the File class is templated. All definitions (and declarations) are in File.hpp

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

// Material.hpp
#ifndef MATERIAL_HPP
#define MATERIAL_HPP

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

Finally the driver code:

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

int main (){
...
}

回答1:

(Complete) specializations of a template are not templates themselves. If you are specializing the function, then you need to either just declare it in the header and provide the implementation in a single translation unit, or else make the definition inline:

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

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

Alternatively:

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


标签: c++ linker clang