C++ Hello World Undefined symbols for architecture x86_64:

假如想象 提交于 2019-12-07 16:36:25

问题


Should be straightforward, but when I compile the C++ Hello World code returns a bunch of undefined symbol errors.

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

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

Heres how I compile: Open terminal, cd to the directory, gcc hello.cpp

Then I get the errors. Any thoughts? I feel like I may have broken somehting... or I am just missing something really obvious. Any help is greatly appreciated!

Heres the errors:

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

回答1:


You need to use g++ to compile and link C++ code:

g++ hello.cpp -o hello

Or to be fully Stack Overflow compliant:

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



回答2:


Use g++ instead of gcc. But anyways your gcc installation seems to be broken. Also are you trying to compile 64bit exec on a 32 bit machine/os?



来源:https://stackoverflow.com/questions/13223992/c-hello-world-undefined-symbols-for-architecture-x86-64

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!