There is something I'm obviously missing generally with this. I bet it's something with symbols. Thanks for any advice, I know it's most probably something I could find out if reading some tutorial so sorry if bothering with stupid stuff again :)
class myClass< public: myClass(); void function(); >;
myClass.cpp:
#include "myClass.h" myClass::myClass()<> void myClass::function()<>
program using the class:
#include "myClass.h" int main()finally I compile it like this:
g++ -o main.exe -L. -l myClass main.cpp
the error is just classic:
C:\Users\RULERO~1\AppData\Local\Temp/ccwM3vLy.o:main.cpp:(.text+0x31): undefined reference to `myClass::myClass()' C:\Users\RULERO~1\AppData\Local\Temp/ccwM3vLy.o:main.cpp:(.text+0x3c): undefined reference to `myClass::function()' collect2: ld returned 1 exit status
2,419 2 2 gold badges 18 18 silver badges 27 27 bronze badges
asked Apr 12, 2010 at 18:06
3,187 5 5 gold badges 33 33 silver badges 50 50 bronze badges
Closely related: stackoverflow.com/a/409470/6064933
Commented Jul 3, 2021 at 13:22
This is probably a link order problem. When the GNU linker sees a library, it discards all symbols that it doesn't need. In this case, your library appears before your .cpp file, so the library is being discarded before the .cpp file is compiled. Do this:
g++ -o main.exe main.cpp -L. -lmylib
g++ -o main.exe main.cpp myClass.lib
The Microsoft linker doesn't consider the ordering of the libraries on the command line.
answered Apr 12, 2010 at 18:40 7,313 3 3 gold badges 31 31 silver badges 34 34 bronze badgesDon't. This catches all kinds of developers out. I probably spent a week trying to figure out strange linker problems in a large project that turned out to be this issue.
Commented Apr 12, 2010 at 18:53You're my hero of the day! :) I spent an hour sitting in front of an undefined reference linker error to find out that it is this simple mistake. Thank you so much :)
Commented Nov 1, 2012 at 23:36Thanks a bunch, just came across this thread having the same problem, and it pretty much solved the issue for me too! :)
Commented Sep 27, 2014 at 11:53Even in cmake framework under linux, it appeared that order inside th target_link_libraries() do matters. Thank you a lot for this indication that saved me a lot of time !
Commented Oct 13, 2015 at 12:43Eleven years later, you saved my day. Many thanks. Both Windows and macOS linkers don't consider the ordering of the libraries. Only Linux..
Commented May 26, 2021 at 8:35Another possible cause: forgetting extern "C" .
I ran into this because I was trying to link a C++ program with a C static library. The library's header didn't have extern "C" so the linker was looking for a mangled function name, and the library actually had the unmangled function name.
It took a while to figure out what was going on, so I hope this helps someone else.
answered Jul 5, 2013 at 17:14 1,161 1 1 gold badge 9 9 silver badges 22 22 bronze badges Thank you, that saved me a lot of time Commented Nov 27, 2017 at 7:17This is an issue how the linker optimizes the output code. Lets assume we have an executable that uses two libraries: Lib_A and Lib_B. Lib_A depends on Lib_B The Lib_A defines symbols: Lib_A1 and Lib_A2, and the Lib_B defines symbol Lib_B1 and Lib_B2. Now lets assume that the executable uses only symbol Lib_A1, and Lib_A1 uses symbol Lib_B1 which is defined in Lib_B. Symbol Lib_B1 is never used in the executable.
g++ -o main.exe main.cpp myClass.lib
Using the library path and the -l flag is fraught with problems, but if you must do it, rename your library to libmylib.a and then compile as:
g++ -o main.exe main.cpp -L. -lmylib
Note also that for portability reasons, it's generally a bad idea to use mixed case in the names of source or output files.