1

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?
pasha
  • 2,035
  • 20
  • 34
  • Line started with # is a preprocessor directive, like `#include` and `#ifdef` etc – Slava Dec 12 '18 at 16:40
  • "Is there any document that I should read" I would start with https://en.wikipedia.org/wiki/C_preprocessor – Slava Dec 12 '18 at 16:43
  • @Slava, the translation unit is generated after preprocessing, so those aren't preprocessor directives. – pasha Dec 12 '18 at 17:21

2 Answers2

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.

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.
Marek R
  • 32,568
  • 6
  • 55
  • 140
0

Line number information.

If you compile the preprocessed output, those can.be used to find the line numbers and files of the original source.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524