30

I am trying to understand the compiling process. We can see the preprocessor intermediate file by using:

gcc -E hello.c -o hello.i

or

cpp hello.c > hello.i

I roughly know what the preprocessor does, but I have difficulties understanding the numbers in some of the lines. For example:

 # 1 "/usr/include/stdc-predef.h" 1 3 4
 # 1 "<command-line>" 2
 # 1 "hello.c"
 # 1 "/usr/include/stdio.h" 1 3 4
 # 27 "/usr/include/stdio.h" 3 4
 # 1 "/usr/include/features.h" 1 3 4
 # 374 "/usr/include/features.h" 3 4

The numbers can help debugger to display the line numbers. So my guess for the first column is the line number for column #2 file. But what do the following numbers do?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Conan
  • 561
  • 5
  • 17
  • Possible duplicate of [What is the meaning of lines starting with a hash sign and number like '# 1 "a.c"' in the gcc preprocessor output?](http://stackoverflow.com/questions/5370539/what-is-the-meaning-of-lines-starting-with-a-hash-sign-and-number-like-1-a-c) – jww Feb 28 '17 at 02:41

1 Answers1

20

The numbers following the filename are flags:

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.

Source: https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thank you. It is very useful information. I am still trying to understand how these fit into the picture. Could you explain a little bit? I will also continue my research based on your source, see how these flags been interpreted. – Conan Oct 12 '15 at 21:05
  • I believe the 1 and 2 flags are meant to give an idea of the nested nature of the include files, so it's apparent who included who. System headers might contain constructs that are non-standard, so the 3 flag tells the compiler that those constructs are acceptable in that context. And since header files could be included by either C or C++ source, the 4 flag lets the C++ compiler know not to perform any name mangling. – dbush Oct 12 '15 at 21:13
  • Recent versions of gcc can use the "nested nature" to show the succession of include-files where a problem lies. It's debatable whether that's useful, since the newer versions haven't offered newer type-checking or better analysis, and as a result simply magnify existing small problems. – Thomas Dickey Oct 12 '15 at 22:03
  • At this point I cannot digest much due to lack of knowledge. Will continue my study. Thanks guys. – Conan Oct 13 '15 at 01:36