您的位置:首页 > 编程语言 > C语言/C++

用g++编译最简单的C++程序hello.C出现可以用-Wno-deprecated屏蔽的警告,想问个究竟

2014-06-24 17:48 393 查看
程序如下:

#include <iostream.h>

main ()

{

cout << "hello, world!" << endl;

}

警告如下:

in file included from /usr/include/c++/3.2.2/backward/iostream.h:31,

from hello.c:1:

/usr/include/c++/3.2.2/backward/backward_warning.h:32:2: warning:

#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 <sstream> instead of

the deprecated header <strstream.h>. to disable this warning use -wno-deprecated.

出现上面的警告是什么原因,要怎么解决才可以不用-wno-deprecated参数直接编译。

网友:blh

c++的程序和c时有区别的,修改如下

#include <iostream>

using namespace std;

main ()

{

cout << "hello, world!" << endl;

}

网友:ybei

<iostream.h>是老的c++函数库,g++建议你使用新的<iostream>

使用方法就像楼上说的

要注意using namespace std 不能省

网友:interbanker

#include <iostream.h>

main ()

{

cout << "hello, world!" << endl;

}

------------------------------------

当使用gcc编译的时候提示出错:

gcc hello.cpp -o hello

undefined first referenced

symbol in file

endl(ostream &) /usr/tmp/cckxbuns.o

ostream::operator<<(ostream &(*)(ostream &))/usr/tmp/cckxbuns.o

cout /usr/tmp/cckxbuns.o

ostream::operator<<(char const *) /usr/tmp/cckxbuns.o

hello: fatal error: symbol referencing errors. no output written to hello

collect2: ld returned 1 exit status

当使用g++编译

g++ hello.cpp -o hello

编译通过,

我把hello.cpp改成hello.c,还是用g++ hello.c -o hello,编译通过

综上所述,编译不通过的原因不是因为命名空间的问题。

因为gcc编译的是c语言,g++编译的是c++程序。

另注:

gcc assumes preprocessed (.i) files are c and assumes c

style linking.

g++ assumes preprocessed (.i) files are c++ and assumes

c++ style linking.

网友:onlymilan

#include <iostream.h> 是window下的

#include <iostream>

using namespace std; 这才是unix/linux下的

网友:fierygnu

呵呵。

老c++用.h做头文件,没有namespace std,新标准规定不要.h,有namespace。这个跟平台没关系,跟编译器实现遵循的标准有关系。

网友:onlymilan

命名空间是新的c++标准中的,新版本的c++编译器支持命名空间,unix/linux下的g++都支持

所以,在unix/linux下这样写

#include <iostream>

using namespace std;

绝对不会有警告。

而在windows vc++ 下这样写

#include <iostream>

using namespace std;

就会出错。如果使用#include <iostream.h>就没问题。

而在unix/linux下使用#include <iostream.h>就会出警告,因为这是老版本的c++标准
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐