C ++ Hello World体系结构x86_64的未定义符号:

时间:2012-11-04 23:18:18

标签: c++ undefined

应该很简单,但是当我编译C ++ Hello World代码时,会返回一堆未定义的符号错误。

// my first program in C++
#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}

下面是我如何编译:打开终端,cd到目录,gcc hello.cpp

然后我得到了错误。有什么想法吗?我觉得我可能已经打破了某些......或者我只是遗漏了一些非常明显的东西。非常感谢任何帮助!

继承错误:

Undefined symbols for architecture x86_64:
"std::cout", referenced from:
      _main in ccfUAf5i.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 ccfUAf5i.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccfUAf5i.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in ccfUAf5i.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

2 个答案:

答案 0 :(得分:6)

您需要使用g++来编译和链接C ++代码:

g++ hello.cpp -o hello

或完全符合Stack Overflow标准:

g++ -W -Wall -Werror -pedantic -o hello hello.cpp

答案 1 :(得分:1)

使用g ++而不是gcc。但无论如何你的gcc安装似乎被打破了。你也试图在32位机器/操作系统上编译64位exec?