c++ undefined references with static library

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 :)

edit:

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

5 Answers 5

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 badges

Don'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:53

You'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:36

Thanks 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:53

Even 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:43

Eleven 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:35

Another 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:17

This 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.

  1. In case of windows, the linker works like this: I have executable with two which uses some libs and all symbols used in executable and all libs are lib_A1 and lib_B1. Thus I will need these two symbols, and the rest is unnecessary. I will undefine lib_A2 and lib_B2
  2. In case of linux if you link Lib_B before Lib_A like this: g++ . -lLib_B -lLib_A The linker works like this: I have executable that first links Lib_B. I do not see that the executable uses symbol Lib_B1 nor Lib_B2. They are unnecesary, thus I will undefine them. Later the linker see. Oh I have another library Lib_A. I can see that executable uses symbol Lib_B1. I will keep it and undefine unused symbol Lib_B2. It does not see that Lib_B1 uses Lib_A1, Which is already undefined.
  3. In case of linux if you link Lib_A before Lib_B like this: g++ . -lLib_A -lLib_B The linker works like this: I have executable that first links Lib_A. Oh, I can see that executable uses Lib_A1. I will keep them and undefine Lib_A2. Later it can see. Oh I have another library Lib_B. I can see that now executable with already linked symbols, uses Lib_B1, I will keep them. As a result it keeps Lib_B1 and Lib_A1, and undefined Lib_B2 and Lib_A2.
answered Aug 24, 2017 at 13:33 Grzegorz Dymek Grzegorz Dymek 131 1 1 silver badge 3 3 bronze badges
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.