Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Hello World Undefined symbols for architecture x86_64:

Tags:

c++

undefined

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
like image 396
Blakedallen Avatar asked Feb 18 '23 14:02

Blakedallen


2 Answers

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
like image 184
Kerrek SB Avatar answered Mar 03 '23 01:03

Kerrek SB


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?

like image 38
Aniket Inge Avatar answered Mar 03 '23 03:03

Aniket Inge