I\'ve been developing a school project in XCode. The final product has to be submitted in source code with a makefile, so I wrote a makefile and to start compiling that way,
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.)
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.
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.
gcc main.o StackList.o world.o Farm.o -g -o Project1
What in this command line do you think tells gcc
to link in the C++ standard library? There are no C++ files being linked. You haven't specified a language. And you invoke the compilter as gcc
.