2016-04-23 208 views
0

我剛開始學習C++。當我使用GCC編譯版本,下面的代碼:GCC(Ubuntu的5.2.1-22ubuntu2)5.2.1 20151010 我得到這個錯誤:C++程序無法編譯

的gcc -o tst.cpp 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 

和這裏是我的代碼:

#include <iostream> 

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

怎麼回事?

+4

@Satya不PRO提示。 – LogicStuff

+0

OP沒有錯過任何'std ::'和[this](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice)。 – LogicStuff

+1

@Satya沒什麼,只是刪除評論。這完全是無意義的。 – juanchopanza

回答

3

你應該使用G ++,不是的GCC編譯C++文件

0

'G ++ TEST.CPP -o測試' 解決了這個問題

+1

雖然這是微不足道的,但對於任何提問的人來說都不是微不足道的。有點爲什麼解釋是需要做出這個答案。再多一個句子就可以做到。 – user4581301