在#include语句中使用垃圾字符时没有编译器错误 [英] No compiler error on using garbage characters in #include statement

查看:242
本文介绍了在#include语句中使用垃圾字符时没有编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream> gfhgfhgf
using namespace std;

int main() {
    return 0;
}

为什么这段代码会编译?根据包含语法的gcc参考:

Why does this code snippet compile? As per The gcc reference on Include Syntax:


如果文件名后面的行上有任何内容(注释除外),则会出错。

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


$ b

and that's exactly what's being done in the code.

推荐答案

使用 -pedantic在 gcc clang 中的-errors 标志将此转换为错误查看活动

Using the -pedantic-errors flags in gcc and clang turns this into an error see it live:

error: extra tokens at end of #include directive
#include <iostream> gfhgfhgf
                    ^

这表示它是扩展名。

如果我们查看接口C和TAL In他们有一些这样的代码:

If we look at the Interfacing C and TAL In The Tandem Environment they have some code like this:

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

因此, gcc clang 支持include指令后的额外字符,以支持某些平台所需的扩展。使用附加标志可使 gcc clang 对违反标准的扩展发出警告,如上所述,您可以使用 -pendatic-errors 将此转换为错误(强调我):

So both gcc and clang support extra characters after the include directive to support an extension needed on some platforms. Using the -pedantic flags makes gcc and clang produce a warning for extensions that violate the standard and as noted above you can use -pendatic-errors to turn this into an error (emphasis mine):


获取所需的所有诊断按标准,您应该
也指定-pedantic(或-pedantic错误,如果你想他们是
错误,而不是警告)。

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).

我们可以在 nolist 扩展名的引用。 hp.com/hpsc/doc/public/display?docId=emr_na-c02128447rel =nofollow>针对NonStop Systrms的HP'sC / C ++程序员指南,其中说:

We can find a reference for the nolist extension in the HP'sC/C++ Programmers guide for NonStop Systrms which says:


nolist
  directs the compiler not to list the contents of the file or sections
  being included.
This is an HP NonStop  extension to the standard.


注意,草案C ++标准定义此形式 include的语法include 部分中

Note, the draft C++ standard defines the grammar for this form of include in section 16.2 [cpp.include] as follows:

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

这篇关于在#include语句中使用垃圾字符时没有编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆