Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - What option do I use to produce a listing file?

Tags:

c

gcc

I am new to the C programming language and gcc.

I am trying to decipher a rather complex C program. I would like to read a helpful listing file instead of the source file.

I am looking for a listing file created by the gcc compiler that contains:

  1. the source code for all the includes
  2. xref = cross reference listing
  3. reference to where the variable is declared. For example, if the line contains i++;, then a note saying were i is declared.

I did a search for this, but gcc has so many options, I got lost.

If there is a better place to ask my question, please let me know.

like image 930
historystamp Avatar asked Apr 27 '12 02:04

historystamp


1 Answers

Well, I AM old-school, and what the OP needs is pre-processor output, and yes it can be more edifying than an IDE. The preprocessor handles all of the # statements, like #include and #ifdef. So it shows you what eventually becomes the input to the compiler. The g++ man page explains the 4 steps: preprocessing, compilation, assembly and linking

and it goes on to explain that the sequence can be stopped at any point. Then under "Preprocessor Options", the way to control this is explained. As another post stated, -E will do the trick, but that is only part of the answer. For finer control use the -f family of options, such as -fdirectives-only. So probably what the OP wants is:

    gcc  -E  -fdirectives-only  -o MySrc.lst  MySrc.cpp

For those using C++, I recommend using g++ directly:

    g++  -E  -fdirectives-only  -o MySrc.lst  MySrc.cpp

The desired listing is then in MySrc.lst

like image 104
Joseph Argenio Avatar answered Sep 17 '22 14:09

Joseph Argenio