Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ compile error

Tags:

c++

g++

ubuntu

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 ...

like image 446
kio Avatar asked Jul 27 '11 15:07

kio


People also ask

What is meant by Compile error?

Compilation error refers to a state when a compiler fails to compile a piece of computer program source code, either due to errors in the code, or, more unusually, due to errors in the compiler itself. A compilation error message often helps programmers debugging the source code.

What does G do in compiling?

(debug) Inserting the `g' flag tells the compiler to insert more information about the source code into the executable than it normally would.

What causes a compile error?

A compile error happens when the compiler reports something wrong with your program, and does not produce a machine-language translation. You will get compile errors.

What is gcc compiler error?

This error message indicates that the compiler has encountered a variable name which does not have a corresponding declaration. It can be caused by a missing declaration, or a typing error in the name. Variable names are case-sensitive, so foo and Foo represent different variables.


2 Answers

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.

like image 195
jcoder Avatar answered Oct 18 '22 13:10

jcoder


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.

like image 32
AmokHuginnsson Avatar answered Oct 18 '22 14:10

AmokHuginnsson