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