4

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
Blakedallen
  • 664
  • 1
  • 8
  • 23
  • 2
    You should be using `g++` to compile your 'C++' programs. Using `g++` will make these errors go away. – Chad Nov 04 '12 at 23:21

2 Answers2

7

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
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 3
    First there was ANSI compliant, then there was ISO compliant. Now Stack Overflow compliant :-P – Aniket Inge Nov 04 '12 at 23:24
  • +1 just for the SO compliance. I'll be fully supporting this if Kerrek proposes it as an add to the gcc/g++ switch-set =P -SOCompliantFTW – WhozCraig Nov 04 '12 at 23:26
  • Hehe. Thanks. The idea is that you shouldn't post a question here unless you're using an SO compliant compiler invocation :-) – Kerrek SB Nov 04 '12 at 23:29
  • "SO compliant compiler invocation"? So `a && b || c && d` should be LISP-ized to `(a && b) || (c && d)` to make gcc happy? Turn off stupid warnings. – Pete Becker Nov 05 '12 at 13:33
  • @PeteBecker: Remember that this is in the context of being confused about code and asking a question on SO. I'd rather you type one inconvenient pair of parentheses than see a hundred trivial questions that would never have existed in the first place had warnings been enabled. – Kerrek SB Nov 05 '12 at 13:49
  • @KerrekSB - I don't agree. Turning on all warnings and turning them into errors encourages bad coding practices. Beginners need to learn how to write good code, not compiler-specific idiosyncratic messes. In particular, too many examples here have **far** too many parentheses (mostly not the result of compiler warnings, though). – Pete Becker Nov 05 '12 at 14:20
  • @KerrekSB - I should add that twenty years ago I agreed with what you say. The problem is that compilers now issue far more warnings than they used to, and far more of them are nanny warnings (you might not be smart enough to use this feature...). – Pete Becker Nov 05 '12 at 15:22
1

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?

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • Ahh, yes the wrong compiler, That is a beginner C++ mistake I would not have caught. Thank you for that. – Blakedallen Nov 04 '12 at 23:25
  • About the architecture, I'm running on intel 64bit, so shouldn't be that problem, maybe my gcc installation is off. – Blakedallen Nov 04 '12 at 23:26
  • @Blakedallen try compiling it with `g++ hello.cpp -o hello` I am pretty sure that if gcc hadn't recognized your file as a C++ file It would have said a lot of undefined symbols during the parsing phase. – Aniket Inge Nov 04 '12 at 23:27