GCC 链接器找不到标准库? [英] GCC linker can't find standard library?

查看:30
本文介绍了GCC 链接器找不到标准库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 make sure I had a working copy. Here's my 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",我收到未定义符号"错误.

Compiling each of the object files works fine, but when it gets to "all," the linking step, it appears to not be aware of the standard library. I get "undefined symbols" error for everythin from "cin", to "basic_string", to "operator new".

我的印象是这些事情不需要直接指出,事实上过去也不需要这样做.

I was under the impression that these things didn't need to be indicated directly, and in fact have not needed to do so in the past.

知道会发生什么吗?

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

If it helps, here's the start of the (very long) error message:

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

推荐答案

要链接C++代码,需要使用g++命令,而不是gcc命令.

To link C++ code, you need to use the g++ command, not the gcc command.

编译时,gcc 命令知道它正在编译 C++ 代码,因为 .cpp 后缀(但最好使用 g++ 命令).

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

链接时,gcc 命令只会看到一堆.o 文件.g++ 命令与 gcc 命令的不同之处在于它知道在哪里可以找到 C++ 特定的库.

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.

(事实上,gcc 这个名称既指通常用于编译 C 代码的命令,也指整个GNU 编译器集合",这一事实可能有点令人困惑.)

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

这篇关于GCC 链接器找不到标准库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆