现在的位置: 首页 > 综合 > 正文

linux 下编译c++y遇到的问题,(gcc只是c 语言编译器,g++是c++编译器)

2014年09月05日 ⁄ 综合 ⁄ 共 3916字 ⁄ 字号 评论关闭
这个问题一直困惑了我好久,一直都没有得到解决,说来真可笑,一个简简单单的hello.cpp最能折腾我这么多天。好了,今天总算是把这问题给解决了,由于考虑到和遇到同样问题的朋友,所以现在我就先把它给写上来,希望能让你们摆脱这种被编译器玩的困境,希望这篇文章能为和我一样奋斗在程式前线的朋友提供一些帮助。
代码 1:
//test.cpp
  #include <iostream.h>
  int  main(){
    cout<<"hello word!"<<endl;
    return 0;
 }
我们编译 一下:

collect2: ld 返回 1
[hqlong@hqlong2008 ~]$ gcc test.cpp
在包含自 /usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/backward/iostream.h:31 的文件中,
                 从 test.cpp:1:
/usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/backward/backward_warning.h:32:2: 警告:#warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
/tmp/ccR8RPmI.o: In function `__static_initialization_and_destruction_0(int, int)':test.cpp:(.text+0x23):对‘std::ios_base::Init::Init()’未定义的引用
/tmp/ccR8RPmI.o: In function `__tcf_0':test.cpp:(.text+0x6c):对‘std::ios_base::Init::~Init()’未定义的引用
/tmp/ccR8RPmI.o: In function `main':test.cpp:(.text+0x8e):对‘std::cout’未定义的引用
:test.cpp:(.text+0x93):对‘std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)’未定义的引用
:test.cpp:(.text+0x9b):对‘std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)’未定义的引用
:test.cpp:(.text+0xa3):对‘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/ccR8RPmI.o:(.eh_frame+0x11):对‘__gxx_personality_v0’未定义的引用
collect2: ld 返回 1
分析一下上面错误的原因,大概是这么几点错误:
  1. 请使用标准的c++头文件,或者用<iostream>来替代<iotream.h> 
  2. 一些在C++里的标准符号显示未定义.
    
        使用#include<iostream>,得到的是置于名字空间std下的iostream库的元素;如果且用#include<iostream.h>,得到的是置于全局空间的同样的元素。在全局空间获取元素会导致名字冲突,而设计名字空间的初衷下是用来避免这种名字冲突的发生。其实没有<iostream.h>这样的东西,标准化委员会在简化非C标准文件时用<iostream>取代了它。但不有完全取消<iostream.h>的使用,并且很多编译器都同时支持<iostream>和<iostream.h>,不无无论那种编译器它肯定都是支持<iostream>。
这下相信大家应该知道是怎么回事了吧。让我们把代码再改一下。
#include<iostream.h> 改成 #include<iostream>
另外还得加上一句:使用名字空间。
using namespace std;
好了,修改后的代码如下:

#include <iostream>
using namespace std;
int  main(){
    cout<<"hello word!"<<endl;

    return 0;
}
再编译一下:
[hqlong@hqlong2008 ~]$ gcc test.cpp
/tmp/ccbcixwH.o: In function `__static_initialization_and_destruction_0(int, int)':test.cpp:(.text+0x23):对‘std::ios_base::Init::Init()’未定义的引用
/tmp/ccbcixwH.o: In function `__tcf_0':test.cpp:(.text+0x6c):对‘std::ios_base::Init::~Init()’未定义的引用
/tmp/ccbcixwH.o: In function `main':test.cpp:(.text+0x8e):对‘std::cout’未定义的引用
:test.cpp:(.text+0x93):对‘std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)’未定义的引用
:test.cpp:(.text+0x9b):对‘std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)’未定义的引用
:test.cpp:(.text+0xa3):对‘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/ccbcixwH.o:(.eh_frame+0x11):对‘__gxx_personality_v0’未定义的引用
collect2: ld 返回 1
怎么还是出现错误呢?还是说什么未定义之类的。
我也是琢磨了好几天也想不明白,这是什么原因,后来被一位仁兄给我指出,原来我们用的是c++编译器,我是记住了,希望你们也要记住,用g++编译,不是用gcc,gcc应该是编译C语言的。
我们再编译一下;
[hqlong@hqlong2008 ~]$ g++ test.cpp
OK,就这样,成功了,不过现在根本没有编译过的文件?因为我们没有把指定编译文件,当然会这样。如果我们指出编译文件,看一下会出现什么效果。
[hqlong@hqlong2008 ~]$ g++ -o test test.cpp
test为我们指定的编译后的输出文件:
我们现在浏览一下文件目录:
[hqlong@hqlong2008 ~]$ ls
aa     c++     Desktop  fpm.odt  manual  mysql+php_apche.odt  Public       RealPlay  temp.mp3  test.cpp   test.php~  
a.out  chmsee  doc      LumaQQ   mysql   mysqlproblem.odt     public_html  smb       test      test.cpp~  xdict
test就是我们刚才编译后生成的编译后的文件。我们现在来显示一下它:
[hqlong@hqlong2008 ~]$ ./test
hello word!
感谢上帝,hellow word!出来了。
就暂且写到这吧,以后如果有什么新的发现,我会一一贴上来的,希望朋友里支持我。

抱歉!评论已关闭.