Saturday 1 April 2017

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



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



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...