GCC linker can't find standard library?
Solution 1:
To link C++ code, you need to use the g++
command, not the gcc
command.
When compiling, the gcc
command knows that it's compiling C++ code because of the .cpp
suffix (but it's a good idea to use the g++
command anyway).
When linking, the gcc
command just sees a bunch of .o
files. The g++
command differs from the gcc
command in that it knows where to find the C++-specific libraries.
(The fact that the name gcc
refers both to the command, which is usually used to compile C code, and the "GNU Compiler Collection" as a whole, can be a little confusing.)
Solution 2:
You need to use g++
to compile and link C++ source code, not gcc
.
Also, you can leave out all targets besides the first and last ones. make
knows how to compile .cpp
files to .o
already -- you don't have to tell it how.
Solution 3:
I know this is old, but if you are compiling and linking C++, you can specify the standard library yourself. Add -lstdc++
at end of your command.