"Undefined symbols" compiler error

Hi,

I am trying out C++ with a small hello world-program:

1
2
3
4
5
6
#include <iostream>
using namespace std;

int main (int argc, char **argv) {
	cout << "Hello world!" << endl;
}


but when I compile it on OS X with "gcc helloworld.cpp", I get an error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Undefined symbols:
  "___gxx_personality_v0", referenced from:
      ___gxx_personality_v0$non_lazy_ptr in cc37hhma.o
  "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
      __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_$non_lazy_ptr in cc37hhma.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in cc37hhma.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
      _main in cc37hhma.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const", referenced from:
      std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in cc37hhma.o
      std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in cc37hhma.o
      std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in cc37hhma.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 cc37hhma.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in cc37hhma.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
      std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in cc37hhma.o
  "std::cout", referenced from:
      __ZSt4cout$non_lazy_ptr in cc37hhma.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


What does this mean? My guess is that the compiler can't find cout from the std namespace? How do I fix it?
closed account (S6k9GNh0)
What setup are you using ">.>?
Last edited on
Try with g++ instead of gcc
What setup are you using ">.>?


What do you mean by "setup"?

Try with g++ instead of gcc


That worked! :) Thanks! Can you explain the difference? I thought gcc was able to compile C++?
closed account (S6k9GNh0)
/me can't believe he didn't see that.
gcc is the GNU C compiler, g++ is the GNU C++ compiler
If you add -lStdC++ you can compile with GCC...
g++ is just a frontend of gcc that automatically links against the c++ runtime library instead of the c runtime library.
Topic archived. No new replies allowed.