views:

320

answers:

2

Maybe I should still be in bed. I woke up wanting to program. At any rate, now I'm getting some linker errors that I'm baffled over. What do you make of all this? I hope I'm not posting too much of it. I was going to post just a piece, but that didn't feel right. I checked some of the header files mentioned in the errors, but I didn't see Split anywhere. Oddly enough it started out named split, but I got similar errors to this.

/home/starlon/Projects/LCDControl/WidgetIcon.h:59: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here
QtDisplay.o: In function `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)':
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/new:101: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here
DrvQt.o: In function `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)':
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/bits/stl_deque.h:79: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here
LCDText.o: In function `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)':
/usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/new:101: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here
Property.o: In function `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)':
/usr/include/QtCore/qatomic_i386.h:125: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here
moc_QtDisplay.o: In function `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)':
/home/starlon/Projects/LCDControl/WidgetIcon.h:59: multiple definition of `LCD::Split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
LCDControl.o:/home/starlon/Projects/LCDControl/WidgetIcon.h:59: first defined here

Here's Split:

std::vector<std::string> Split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    return elems; //Split(s, delim, elems);
}
+3  A: 

A usual cause for multiple definitions errors like this is when you define the function in a header file without the inline keyword. Also, if the Split function you posted is from a LCD class the signature is missing the LCD:: part.

ltcmelo
Inline did it. Learn something new everyday. :)
Scott
I was hoping Split was defined withing the class declaration (the lack of LCD::...). Remember, defining the function within the class declaration default inlines the same.
dirkgently
Good. If you don't want to inline it, just place the definition in a implementation (.cpp, .cxx, etc) file.
ltcmelo
Itcmelo: Are there any drawbacks to inline?
Scott
Scott: For more information about the inline keyword, see here: http://www.parashift.com/c++-faq-lite/inline-functions.html
Christian
+2  A: 

More than one object file is providing the implementation for LCD::Split. This is because the definition is in a header file.

Inlining, or making the function static, will restrict the visibility to each object file that uses it, and prevent clashes. However this will mean that each and every object file will contain the implemenation for that function if it's used. Furthemore, inlining will increase the amount of generated binary at every callsite. Making the function static, will create a single copy of LCD::Split for each object file.

The best solution is to put the implementation for LCD::Split, in its own source file, so that the implementation is present in a single object file after compilation.

Furthermore, you may want to ensure that your header contains an include guard, such as #pragma once, to prevent compile time conflicts for multiple declarations.

Matt Joiner