Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate Symbol Linker Error (C++ help)

I'm learning some CSP (constraint satisfaction) theory stuff right now, and am using this library to parse XML files. I'm using Xcode as an IDE.

My program compiles fine, but when it goes to link the files, I get a duplicate symbol error with the XMLParser_libxml2.hh file. My files are separated as such:

A class header file that includes the XMLParser file above
A class implementation file that include the class header file
A main file that includes the class header file

The duplicate symbol is occurring in main.o and classfile.o, but as far as I can tell, I'm not actually adding that .hh file twice.

Full error:

ld: duplicate symbol bool CSPXMLParser::UTF8String::to<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >&) constin
/Users/vash265/CSP/Untitled/build/Untitled.build/Debug/Untitled.build/Objects-
normal/x86_64/dStructFill.o and
/Users/vash265/CSP/Untitled/build/Untitled.build/Debug/Untitled.build/Objects-
normal/x86_64/main.o`

Copying the implementation of the class into the main file and taking the class implementation file out of the compilation target removes the error, but it's a disorganized mess this way, and I'll be adding more classes very soon (and it would be nice to have them in separate files).

As I've come to understand it, this is caused by the file (XMLParser_libxml2.hh) having both the class and function definition and implementation in one file (and it seems as though this might have been necessary due to the use of templates in that 'header' file). Any ideas on how to get around sticking all my class files in my main.cpp? (I've tried #ifdefs, they don't work).

like image 422
Vash265 Avatar asked Jun 02 '10 00:06

Vash265


1 Answers

James was correct. The three template functions inside the header file needed to be declared inline for it to link properly. Thanks!

like image 161
vash265 Avatar answered Nov 15 '22 10:11

vash265