#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