9

I'm a newbie in Ubuntu. I tried to compile a simple "Hello World!" c++ code in Ubuntu 11.04, with that code (in terminal):

gcc -Wall -W -Werror tex.cpp -o tex. 

but compiler returned a lot of errors :

/tmp/ccL8c1p8.o: In function `main':
tex.cpp:(.text+0xa): undefined reference to `std::cout'
tex.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*)'
/tmp/ccL8c1p8.o: In function `__static_initialization_and_destruction_0(int, int)':
tex.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
tex.cpp:(.text+0x42): undefined reference to `std::ios_base::Init::~Init()'
collect2: ld returned 1 exit status
mohrd@mio:~$ gcc -Wall -W -Werror tex.cpp -o tex.
/tmp/ccEke5JP.o: In function `main':
tex.cpp:(.text+0xa): undefined reference to `std::cout'
tex.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*)'
/tmp/ccEke5JP.o: In function `__static_initialization_and_destruction_0(int, int)':
tex.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
tex.cpp:(.text+0x42): undefined reference to `std::ios_base::Init::~Init()'
collect2: ld returned 1 exit status

simple code :

#include <algorithm>
#include <iostream>
using namespace std;

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

Why? and what should I do???

many thanks ...

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
kio
  • 101
  • 1
  • 1
  • 3

5 Answers5

34

You need to use the g++ (or c++) command to compile your program, using "gcc" will compile it as c++ due to the .cpp extension but not link in the required c++ libraries.

jcoder
  • 29,554
  • 19
  • 87
  • 130
6

Use g++ instead of gcc for compiling C++ code:

g++ -Wall -W -Werror tex.cpp -o tex

gcc does not link stdc++ library by default which is required for your code.

AmokHuginnsson
  • 1,174
  • 10
  • 14
4

Use the g++ command instead of gcc.

highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
1

Try:

g++ -Wall hello.cpp -o hello

Beacuse you are trying to compile C++ code, not C code!

-Wall means Warning ALL, so you'll already see all the warnings

Also, name your programs and source files meaningfully!

LE: of course -Werror remains your choice

LE2: no need of #include <algorithm> in your code, so you can remove it!

Paul
  • 20,883
  • 7
  • 57
  • 74
-1

I was getting the same error for creating a giant arrary (50005) items. Be careful