日期:2014-05-16  浏览次数:20719 次

请问这个简单的程序为什么不能编译?

#include <iostream>
//using   namespace   std;
int   main()
{
                std::cout < < "Hello " < <std::endl   ;
                return   0   ;
}

改成:

#include <iostream>
using   namespace   std;
int   main()
{
                cout < < "Hello " < <endl   ;
                return   0   ;
}

也不行。
出错信息如下

/tmp/cchgmtcg.o:   In   function   `main ':
/tmp/cchgmtcg.o(.text+0x14):   undefined   reference   to   `std::basic_ostream <char,   std::char_traits <char>   > &   std::endl <char,   std::char_traits <char>   > (std::basic_ostream <char,   std::char_traits <char>   > &) '
/tmp/cchgmtcg.o(.text+0x21):   undefined   reference   to   `std::cout '
/tmp/cchgmtcg.o(.text+0x26):   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/cchgmtcg.o(.text+0x2f):   undefined   reference   to   `std::basic_ostream <char,   std::char_traits <char>   > ::operator < <(std::basic_ostream <char,   std::char_traits <char>   > &   (*)(std::basic_ostream <char,   std::char_traits <char>   > &)) '
/tmp/cchgmtcg.o:   In   function   `__static_initialization_and_destruction_0(int,   int) ':
/tmp/cchgmtcg.o(.text+0x5c):   undefined   reference   to   `std::ios_base::Init::Init[in-charge]() '
/tmp/cchgmtcg.o:   In   function   `__tcf_0 ':
/tmp/cchgmtcg.o(.text+0x8b):   undefined   reference   to   `std::ios_base::Init::~Init   [in-charge]() '
/tmp/cchgmtcg.o(.eh_frame+0x11):   undefined   reference   to   `__gxx_personality_v0 '


------解决方案--------------------
从后面来看,是连接时问题,跟头文件已经没关系了。
换句话说,不是编译失败,而是连接失败。
貌似环境没搭建好。
------解决方案--------------------
你该不会是用gcc命令编译的吧
应该是g++
------解决方案--------------------
对,用g++命令编译就对了。