g++ -E 生成的翻译单元中以#符号开头的行是什么

What do the lines starting with # symbol in g++ -E generated translation unit

我试图检查为简单的 hello world 程序生成的翻译单元。 所以,我在 test.cpp.

中写了下面的代码
#include <iostream>
using namespace std;

int main()
{
    cout<<"Hello World"<<endl;
}

然后我用带有 -E 选项的 g++ 编译了上面的文件,并将数据输出到一个临时文件。 该文件包含 C++ 代码,中间的行以 # 符号开头。

类似下面的内容,

# 1 "test.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "test.cpp"
# 1 "/usr/include/c++/8/iostream" 1 3
# 36 "/usr/include/c++/8/iostream" 3
  1. 这些行是什么意思?
  2. 是否有任何我应该阅读的文档,或者我是否必须获得任何特定主题的知识才能理解此文件?

http://tigcc.ticalc.org/doc/comopts.html

-E

Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.

Input files which don't require preprocessing are ignored.

然后你可以找到"Preprocessor Output" in gcc documentation:

# linenum filename flags

These are called linemarkers. They are inserted as needed into the output (but never within a string or character constant). They mean that the following line originated in file filename at line linenum. filename will never contain any non-printing characters; they are replaced with octal escape sequences.

After the file name comes zero or more flags, which are ‘1’, ‘2’, ‘3’, or ‘4’. If there are multiple flags, spaces separate them. Here is what the flags mean:

  • ‘1’ This indicates the start of a new file.

  • ‘2’ This indicates returning to a file (after having included another file).

  • ‘3’ This indicates that the following text comes from a system header file, so certain warnings should be suppressed.
  • ‘4’ This indicates that the following text should be treated as being wrapped in an implicit extern "C" block.

行号信息。

如果编译预处理输出,那些can.be用于查找原始源代码的行号和文件。