GCC linker can't find standard library?

前端 未结 4 1989
臣服心动
臣服心动 2020-11-27 22:29

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,

相关标签:
4条回答
  • 2020-11-27 23:05

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

    0 讨论(0)
  • 2020-11-27 23:09

    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.

    0 讨论(0)
  • 2020-11-27 23:10

    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.

    0 讨论(0)
  • 2020-11-27 23:10
        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.

    0 讨论(0)
提交回复
热议问题