Zos*_*oso 9 c++ compiler-errors c-preprocessor
#include <iostream> gfhgfhgf
using namespace std;
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码片段会被编译?根据包含语法的gcc参考:
如果文件名后面的行上有任何内容(注释除外),则会出错.
而这正是代码中正在做的事情.
Sha*_*our 12
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
^
Run Code Online (Sandbox Code Playgroud)
which indicates it is an extension.
If we look at the Interfacing C and TAL In The Tandem Environment they have some code like this:
#include <stdlibh> nolist
^^^^^^
Run Code Online (Sandbox Code Playgroud)
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):
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).
We can find a reference for the nolist extension in the HP'sC/C++ Programmers guide for NonStop Systrms which says:
Run Code Online (Sandbox Code Playgroud)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.
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
Run Code Online (Sandbox Code Playgroud)