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

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

问题描述

我在XCode开发了一个学校项目。最终产品必须以makefile的形式提交源代码,所以我写了一个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,得到undefined symbols错误。

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

我的印象是,

有什么想法可能发生什么?

Any idea what might be happening?

编辑:

如果有帮助,这里是(很长)错误信息的开始:

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 Compiler Collection作为一个整体,可能有点混乱。)

(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天全站免登陆