2011-12-11 46 views
0

使用B. Stroustrup的编程文本和关于第50页的'你好世界'程序给出了错误。我遇到了“std_lib_facilities.h”包含文件的问题。极端新手 - 你好世界错误

In file included from /usr/include/c++/4.4/ext/hash_map:60, 
      from std_lib_facilities.h:34, 
      from hworld1.cpp:1: 
/usr/include/c++/4.4/backward/backward_warning.h:28: warning: #warning This file  includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. 
/tmp/ccpwXUYx.o: In function `main': 
hworld1.cpp:(.text+0x14): undefined reference to `std::cout' 
hworld1.cpp:(.text+0x19): 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/ccpwXUYx.o: In function `__static_initialization_and_destruction_0(int, int)': 
hworld1.cpp:(.text+0x41): undefined reference to `std::ios_base::Init::Init()' 
hworld1.cpp:(.text+0x46): undefined reference to `std::ios_base::Init::~Init()' 
/tmp/ccpwXUYx.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0' 
collect2: ld returned 1 exit status 

任何建议如何解决这个文件,可能注释掉的哈希部分 -

运行(根)“GCC hworld1.cpp”输出后?

兴奋终于有时间开始,但这似乎比第一次故障排除任务稍大。我曾尝试使用iostream作为包含。我在Ubuntu 11.04上运行这个。也许我需要更新gcc或使用g ++。不知道什么可能让我放弃这一点。我试着用'使用...... std'与...作为我无法想起的合适名称(oops)。任何人都知道正确的包括。

这里是代码 -

#include "std_lib_facilities.h" 

int main() 
{ 
    cout<<"hiya people\n"; 
    return 0; 
} 
+0

使用'g ++'编译并链接C++。 – Mat

+0

@Mark:Stroustrup有[多本书](http://www.stroustrup.com/Programming/)。 – ybungalobill

+0

请不要以root身份执行此操作。 – Jookia

回答

6

你应该使用g ++,因为gcc是C编译器(不是C++)。

+0

这是真的。虽然gcc是一个C++编译器,但它不会自动链接到stdlib。 – 111111

+0

谢谢,我可以继续我的学习。很棒的评论 - – user916843

2

这应该修复它。

#include <iostream> 

int main() 
{ 
    std::cout <<"hello world!" << std::endl; 
    return 0; 
} 

这就是说,但本书可能希望你以某种方式做它的原因。这些错误是链接时间错误,在代码编译完成之后,它需要链接其他编译代码以及头文件,应该有一个lib文件。您需要将其添加为编译器参数。

编辑:经过进一步检查,似乎缺少“链接”是标准库,这并不令人惊讶,因为您使用的是gcc而不是g ++,它会自动链接stdlib。

+0

@Mat:是我这样做的方式。我刚刚抽出并粘贴到我的答案中。 – 111111

+2

'endl'和'\ n'不一样,除非你真的想刷新缓冲区,否则你不应该使用它。在出口附近冲洗是多余的。 –

+0

我意识到了这一点,尽管我认为在hello world的情况下你会想要冲洗缓冲区。 (虽然我想像缓冲区在流析构函数中冲刷) – 111111

1

使用头文件iostream而不是你正在使用的头文件。

另外请确保你使用g ++而不是gcc。由于gcc编译器用于C,而g ++用于C++