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 ...
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.
(debug) Inserting the `g' flag tells the compiler to insert more information about the source code into the executable than it normally would.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With