0

I have just started learning c++. When i compile the following code using GCC version: gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010 i get this errors:

gcc tst.cpp -o mytst

/tmp/ccGA15Qf.o: In function `main':
tst.cpp:(.text+0xa): undefined reference to `std::cout'
tst.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
tst.cpp:(.text+0x19): undefined reference to `std::cout'
tst.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccGA15Qf.o: In function `__static_initialization_and_destruction_0(int, int)':
tst.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::Init()'
tst.cpp:(.text+0x5b): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

and here is my code:

#include <iostream>

int main ()
{
  std::cout << "Hello World! ";
  std::cout << "I'm a C++ program";
}

What is going wrong?

2 Answers2

3

You should use g++, not gcc for compiling c++ files

granmirupa
  • 2,780
  • 16
  • 27
0

'g++ test.cpp -o test' solved the problem

  • 1
    While this is trivial, it isn't trivial to anyone asking the question. A bit of a why explanation is needed to make this an answer. One more sentence would do it. – user4581301 Apr 23 '16 at 14:36