C++ 体系结构 x86_64 的未定义符号:我应该使用哪种体系结构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8034568/
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 17:54:06  来源:igfitidea点击:

Undefined symbols for architecture x86_64: Which architecture should I use?

c++macos

提问by oskob

I'm trying to do some really simple stuff in C++, but I can't find any information on how to tackle this. Even the book I have just says "Just compile and run the program".

我正在尝试用 C++ 做一些非常简单的事情,但我找不到任何关于如何解决这个问题的信息。甚至我刚刚说的那本书“只需编译并运行程序”。

test.cpp

测试.cpp

#include <iostream> 
using namespace std;

int main() 
{ 
    cout << "Never fear, C++ is here!"; 
    return 0;
}

The compiler says:

编译器说:

Undefined symbols for architecture x86_64:
  "std::cout", referenced from:
      _main in ccVfJHGs.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      _main in ccVfJHGs.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccVfJHGs.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in ccVfJHGs.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

I tried compiling with flags like -arch i386and -m32but it always says it's the wrong architecture. Which one should I use?

我试图像标志编译-arch i386-m32,但它总是说,这是错误的架构。我应该使用哪一种?

I'm doing this on a Mac but not using XCode, just gcc.

我在 Mac 上执行此操作,但不使用 XCode,仅使用 gcc。

回答by Some programmer dude

The error isn't that it's the wrong architecture, it's that std::cout(and other symbols) isn't defined.

错误不是它的架构错误,而是std::cout(和其他符号)没有定义。

You should compile and link with g++not gcc, to automatically link with correct C++ libraries.

您应该使用g++not编译和链接gcc,以自动链接到正确的 C++ 库。

回答by moshbear

The error is caused because you're compiling with gcc, which only default-links libc. You need to compile with g++so that libstdc++is auto-linked in too.

该错误是因为您正在使用 进行编译gcc,而仅使用 default-links libc。您需要编译,g++以便它libstdc++也自动链接。

回答by Muhammed Shibin

Use g++ instead of gcc to link with exact c++ libraries

使用 g++ 而不是 gcc 来链接精确的 c++ 库