c++g++translation-unit

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


I tried to check the translation unit generated for a simple hello world program looks like. So, I wrote below code in test.cpp.

#include <iostream>
using namespace std;

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

I then compiled the above file with g++ with -E option and output the data to a temp file. The file has c++ code with lines in between starting with # symbols.

Something like below,

# 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. What do these lines mean ?
  2. Is there any document that I should read or do I have to get knowledge in any specific subject to understand this file?

Solution

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

    Then you can find "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.