Take the 2-minute tour ×

Hey, I'm trying to compile some c++ files. I'm getting the following types of errors at the linking phase (the compiling is working) -

Main.o: In function operator<<(std::basic_ostream<char, std::char_traits<char> >&, Set const&)':
/usr/include/c++/4.3/new:105: multiple definition of
operator<<(std::basic_ostream< char, std::char_traits >&, Set const&)' bundleXX.o:/xxx/DATASTRUCTURES/SET/set.h:162: first defined h ere

Main.o: In function print(std::basic_string<char, std::char_traits<char>, std::allo cator<char> >, int)':
/xxx/misc.h:28: multiple definition of
print(std::basic_string, std::allocator >, int)' bundleXX.o:/xxx/misc.h:28: first defined here

This code was linking correctly before. It's only when I tried to interface this code with some other code that stuff blew up.

My makefile is this :-

SRC = Main.C Bundle.C bundleXX.C BMinQuad.C MinQuad.C QPBundle.C OBJ = Main.o Bundle.o MinQuad.o BMinQuad.o QPBundle.o bundleXX.o general_includes.o CCC = g++ EXE = main OPT = -O3 -fomit-frame-pointer -fno-strict-aliasing DEBUG = -g -DDEBUG


Main.o: $(SRC) $(CCC) $(DEBUG) $(LFLAGS) -I /xxx/ -I /xxx/DATASTRUCTURES/ -c $(SRC)

main: $(OBJ) $(CCC) $(OBJ) -I /xxx/ -I /xxx/DATASTRUCTURES/ -o $(EXE) /bin/rm -rf main.o

clean: /bin/rm -rf main.o /bin/rm -rf $(EXE) touch out.out /bin/rm -rf *.out

I would greatly appreciate some help.


Edit: I tried inlining but it didn't work. I finally minimized the number of times I was including my code in the new code and it finally linked correctly. I think it might have been a case of recursive includes. Thanks for the help though.

share|improve this question

closed as too localized by Shog9 Feb 23 '13 at 2:51


3 Answers 3

Are the definitions of the print(std::string, int) and operator<< functions defined inside headers that you are including in multiple C files.

try adding inline before those functions, or defining the functions inside of one of your C files you are linking.

Edit: Your edit mentioned you are doing "recursive" includes: Do you have #ifdef guards around your include files? This will prevent the same translation unit from including the same header multiple times.

#ifndef __MY_FILENAME_H_
#define __MY_FILENAME_H_
// your header details
#endif // End of the file...

The define can be anything as long as its unique to the header file. Just follow some sort of standard.

share|improve this answer

Multiple definitions in the link phase is almost always caused by having code in header files but I'm not sure that's the case here.

Have a look at line 162 of xxx/DATASTRUCTURES/SET/set.h and line 28 of xxx/misc.h (post them here, with a few lines either side for context, for further help).

That appears to be where the conflict is, based on the messages you're getting. It may well be that your new stuff that you're interfacing with shares a type name with some of your current stuff.

share|improve this answer

In misc.h and set.h do you actually define the functions mentioned in the linker error, or do you just declare them as stubs?

If you actually define them, (i.e. if you actually implement function bodies) then you're likely to get multiple definition problems during the linker phase. You can either define them in a separate .cpp file, or else declare them using the inline keyword. The latter option is better if they are very short functions.

share|improve this answer
