C++ GCC 链接器找不到标准库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8904722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:17:41  来源:igfitidea点击:

GCC linker can't find standard library?

c++linkerc++-standard-library

提问by xanderflood

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:

我一直在用 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

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

编译每个目标文件都可以正常工作,但是当它进入“全部”链接步骤时,它似乎不知道标准库。对于从“cin”到“basic_string”到“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.

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

Any idea what might be happening?

知道会发生什么吗?

EDIT:

编辑:

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

回答by Keith Thompson

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

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

When compiling, the gcccommand knows that it's compiling C++ code because of the .cppsuffix (but it's a good idea to use the g++command anyway).

编译时,该gcc命令知道它正在编译 C++ 代码,因为.cpp后缀(但g++无论如何使用该命令是个好主意)。

When linking, the gcccommand just sees a bunch of .ofiles. The g++command differs from the gcccommand in that it knows where to find the C++-specific libraries.

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

(The fact that the name gccrefers 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既指通常用于编译 C 代码的命令,又指整个“GNU 编译器集合”,这可能有点令人困惑。)

回答by duskwuff -inactive-

You need to use g++to compile and link C++ source code, not gcc.

您需要使用g++来编译和链接 C++ 源代码,而不是gcc.

Also, you can leave out all targets besides the first and last ones. makeknows how to compile .cppfiles to .oalready -- you don't have to tell it how.

此外,您可以省略除第一个和最后一个目标之外的所有目标。make知道如何编译.cpp文件.o——你不必告诉它如何编译。

回答by David Schwartz

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

What in this command line do you think tells gccto 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.

您认为该命令行中的哪些内容告诉gcc要在 C++ 标准库中进行链接?没有链接 C++ 文件。您尚未指定语言。并且您将编译器作为gcc.