【问题标题】:No compiler error on using garbage characters in #include statement在#include 语句中使用垃圾字符没有编译器错误
【发布时间】:2015-01-07 10:15:26
【问题描述】:
#include <iostream> gfhgfhgf
using namespace std;

int main() {
    return 0;
}

为什么这段代码 sn-p 可以编译?根据The gcc reference on Include Syntax

如果文件名后面的行有任何内容(除了 cmets),则为错误。

这正是代码中所做的。

【问题讨论】:

标签: c++ compiler-errors c-preprocessor


【解决方案1】:

gccclang 中使用-pedantic-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
                   ^^^^^^

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

获得标准要求的所有诊断,您应该 还指定 -pedantic (或 -pedantic-errors 如果您希望它们是 错误而不是警告)。

我们可以在HP'sC/C++ Programmers guide for NonStop Systrms 中找到nolist 扩展名的参考,上面写着:

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.

注意,draft C++ standard16.2 部分[cpp.include] 中定义了include 的这种形式的语法如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    相关资源
    最近更新 更多