加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在#include语句中使用垃圾字符时无编译器错误

发布时间:2020-12-16 03:17:19 所属栏目:百科 来源:网络整理
导读:#include iostream gfhgfhgfusing namespace std;int main() { return 0;} 为什么这段代码段编译?根据The gcc reference on Include Syntax: It is an error if there is anything (other than comments) on the line after the file name. 这正是代码中正
#include <iostream> gfhgfhgf
using namespace std;

int main() {
    return 0;
}

为什么这段代码段编译?根据The gcc reference on Include Syntax:

It is an error if there is anything (other than comments) on the line after the file name.

这正是代码中正在做的.

解决方法

使用gcc和clang中的-anticantic-errors标志将其转为错误 see it live:
error: extra tokens at end of #include directive
#include <iostream> gfhgfhgf
                    ^

这表明它是一个扩展.

如果我们看看Interfacing C and TAL In The Tandem Environment他们有一些这样的代码:

#include <stdlibh> nolist
                   ^^^^^^

所以gcc和clang都可以在include指令之后支持额外的字符,以支持某些平台上需要的扩展.使用-pedantic flags使gcc和clang对违反标准的扩展产生警告,如上所述,您可以使用-pendatic错误将其转换为错误(强调我的):

to obtain all the diagnostics required by the standard,you should
also specify -pedantic (or -pedantic-errors if you want them to be
errors rather than warnings).

我们可以在HP’sC/C++ Programmers guide for NonStop Systrms中找到诺尔扩展的参考,它说:

06002

注意,draft C++ standard在第16.2节[cpp.include]中定义了这种形式的包含的语法,如下所示:

# include < h-char-sequence> new-line

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读