6

我一直在 XCode 中开发一个学校项目。最终产品必须以源代码和 makefile 的形式提交,所以我编写了一个 makefile 并开始编译,以确保我有一个工作副本。这是我的生成文件:

all: main.o StackList.o world.o Farm.o
        gcc main.o StackList.o world.o Farm.o -g -o Project1

main.o:
        gcc -g -c main.cpp

StackList.o:
        gcc -g -c Stacklist.cpp

world.cpp:
        gcc -g -c world.cpp

Farm.cpp:
        gcc -g -c Farm.cpp

clean:
        rm *.o Project1

编译每个目标文件都可以正常工作,但是当它到达“全部”链接步骤时,它似乎不知道标准库。从“cin”到“basic_string”再到“operator new”,我收到“未定义符号”错误。

我的印象是这些东西不需要直接表示,实际上过去也不需要这样做。

知道会发生什么吗?

编辑:

如果有帮助,这是(非常长的)错误消息的开始:

Undefined symbols for architecture x86_64:
  "std::cin", referenced from:
  _main in main.o
  "std::cout", referenced from:
      _main in main.o
      Farm::print(int)  in Farm.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
      _main in main.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in main.o
      __static_initialization_and_destruction_0(int, int)in StackList.o
      __static_initialization_and_destruction_0(int, int)in world.o
      __static_initialization_and_destruction_0(int, int)in Farm.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in main.o
      ___tcf_0 in StackList.o
      ___tcf_0 in world.o
      ___tcf_0 in Farm.o
  "operator new(unsigned long)", referenced from:
      doStackSearch(std::basic_istream<char, std::char_traits<char> >*, std::list<Farm*, std::allocator<Farm*> >*&)in world.o
4

4 回答 4

23

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

于 2012-01-18T03:18:15.440 回答
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.

于 2012-01-18T03:18:25.963 回答
1

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.

于 2020-11-02T13:07:25.180 回答
-2
    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.

于 2012-01-18T03:21:35.160 回答