4

I am calling on the crowd to help me understand how to read the preprocessor output. I am attempting to go through an exercise of going through the compilation process of a simple C application on Ubuntu 18.04.

The code simpler.c

#include "simpler.h"
int main()
{
      // This is a comment
      return 0;
}

for simpler.h

int y;

I then run the command

username$ cpp simpler.c simpler_cpp

This then produces the preprocessed c file as follows

# 1 "simpler.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "simpler.c"

# 1 "simpler.h" 1
int y;
# 3 "simpler.c" 2

int main()
{

 return 0;
}

Looking this over I am not sure I follow how to read this file? Or at least put it in terms I understand. I do however see that my comment is not there any more, and that I have my line from my header file there. But other than that this is not too clear.

I am attempting to tell myself a "story" with this file, such as "The # 1 is an preprocessor index of the file, so there are # 1, # 31, # 32, and # 3 or 4 files total". That the "# 31 "" means something... I really don't know what?

If anyone can help me interpret this file, I would greatly appreciate it.

I have been attempting to follow the page http://gcc.gnu.org/onlinedocs/cpp/index.html#Top but it reads more like an encyclopedia, which although good if you know the road map, if you are starting from square one it becomes more challenging.

Possible Answer: Thanks for the responses guys, according to https://gcc.gnu.org/onlinedocs/gcc-9.1.0/cpp/Preprocessor-Output.html#Preprocessor-Output

when reading the output of the preprocessed c file, first macros are expanded, comments are removed, and long runs of blanks lines are discarded.

I read the line
# 1 "simpler.c" is a linemarker and means that the following line originated in file "simpler.c" at line 1. After the file there are no flags, which is not described in the file.

The other thing I can do is look at the line
# 1 "simpler.c"
and say the file simpler.c exists, and then ignore it. Which is probably the most practical thing to do. Except I wonder what the
# 1 "built-in"
# 1 "command-line"
mean? if I ignore these then I get something that looks like

int y;


int main()
{

 return 0;
}

Which is what I originally expected from the description of cpp.

Last edit for the day. One thing I have found is the command
cpp -P simpler.c simpler_cpp give the output

int y;
int main()
{
 return 0;
}

There is a no #line flag in the man page that outputs the preprocessed file without any line information. I am guessing that this is really the only output that does matter. I am guessing that this output should have a .i extension, but I don' tknow . Oh well, I hope this is useful to anyone else. If I find any good information out there I will try and write something up.

DisplayName001
  • 253
  • 2
  • 3
  • 8
  • 2
    How about this: https://gcc.gnu.org/onlinedocs/gcc-9.1.0/cpp/Preprocessor-Output.html#Preprocessor-Output ? – Eugene Sh. Jun 11 '19 at 14:32
  • 2
    `# N "file"` marks line number `N` in the given file. The suffix number tells you something about the state of processing of the file (along the lines of: 1 start, 2 finish, 3 and 4 related to nested included files). Or simply ignore the `#` lines; they're 'commentary' on the preprocessor output. – Jonathan Leffler Jun 11 '19 at 14:41

1 Answers1

4

The lines starting with # exist so the compiler can report errors on the correct file and line. The best thing to do is ignore them . In between them is C code with all macros and include files expanded. Typically one reads preprocessor output to debug macros, but your sample has none.

I guess this is a homework assignment to demonstrate include files.

Joshua
  • 40,822
  • 8
  • 72
  • 132