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 (){
...
}