C++: Multiple definition error for global functions in the header file

Aquarius_Girl picture Aquarius_Girl · May 21, 2011 · Viewed 15.6k times · Source

This function is global and is defined in the header file (temporarily I want to keep it there).

The header file also constitutes a particular class which has inline functions and one of those functions call this global function.

The source file doesn't contain any occurrences of the global function in question.

Any hints on cause of the error?

I can post the code if anyone is interested.

mainwindow.o: In function `tileForCoordinate(double, double, int)':
mainwindow.cpp:(.text+0x310): multiple definition of `tileForCoordinate(double, double, int)'
main.o:main.cpp:(.text+0xd0): first defined here
moc_mainwindow.o: In function `qHash(QPoint const&)':
moc_mainwindow.cpp:(.text+0x0): multiple definition of `qHash(QPoint const&)'
main.o:main.cpp:(.text+0x0): first defined here
moc_mainwindow.o: In function `tileForCoordinate(double, double, int)':
moc_mainwindow.cpp:(.text+0x150): multiple definition of `tileForCoordinate(double, double, int)'
main.o:main.cpp:(.text+0xd0): first defined here
collect2: ld returned 1 exit status
make: *** [SimpleRouting] Error 1

Answer

user2100815 picture user2100815 · May 21, 2011

mark it as inline:

 inline void globalfunc() { 
 }

although doing so means that it will no longer strictly be global - you will get a copy in each translation unit that uses the header, but the linker won't object to this.