c++ undefined references with static library

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

or

g++ -o main.exe main.cpp myClass.lib

The Microsoft linker doesn't consider the ordering of the libraries on the command line.


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.