Generate intermediate files ( assembling, preprocessing) in gcc

It is good to see the output files generated by GCC at its each ( preprocessing, assembling..etc) stage.. You will get an idea on  different processes involved and the result worked out by GCC to make a binary/ELF..There are different ways to generate these output files by specifying GCC options..That said, it is possible to generate only assembly output of your source by below way:

#gcc -S -c program.c


[root@humbles-lap gcc]# ls

[root@humbles-lap gcc]# file program.c
program.c: ASCII text
[root@humbles-lap gcc]# gcc -S -c program.c
[root@humbles-lap gcc]# ls
program.c  program.s
[root@humbles-lap gcc]# file program.s
program.s: ASCII assembler program text
[root@humbles-lap gcc]# cat program.s |head -n 5
    .file    "program.c"
    .section    .rodata
    .align 8
    .string    "\n I am always  an example program"
[root@humbles-lap gcc]#How-ever it wont give you all the resulted files in each stage , for ex: preprocessor files. For that, I use below gcc option to generate different output files of my source..#man gcc

           Store the usual “temporary” intermediate files permanently; place them in the current directory and name them based on
           the source file.  Thus, compiling foo.c with -c -save-temps would produce files foo.i and foo.s, as well as foo.o.
           This creates a preprocessed foo.i output file even though the compiler now normally uses an integrated preprocessor.

           When used in combination with the -x command line option, -save-temps is sensible enough to avoid over writing an input
           source file with the same extension as an intermediate file.  The corresponding intermediate file may be obtained by
           renaming the source file before using -save-temps.

           If you invoke GCC in parallel, compiling several different source files that share a common base name in different
           subdirectories or the same source file compiled for multiple output destinations, it is likely that the different
           parallel compilers will interfere with each other, and overwrite the temporary files.  For instance:

                   gcc -save-temps -o outdir1/foo.o indir1/foo.c&
                   gcc -save-temps -o outdir2/foo.o indir2/foo.c&

           may result in foo.i and foo.o being written to simultaneously by both compilers.

#end of man gcc

As mentioned in “man’ page it will generate “.i”, “s”, “.o” files ..


[root@humbles-lap gcc]# ls
[root@humbles-lap gcc]# gcc -o program program.c --save-temps

[root@humbles-lap gcc]# ls
program  program.c  program.i  program.o  program.s
[root@humbles-lap gcc]# file program.i
program.i: ASCII C program text
[root@humbles-lap gcc]# file program.s
program.s: ASCII assembler program text
[root@humbles-lap gcc]# file program.o
program.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
[root@humbles-lap gcc]# file program
program: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, not stripped
[root@humbles-lap gcc]#Examine those “intermediate files” and try to make sense out of it.. it is funny..I will write more about each stage in another blog..

2 thoughts on “Generate intermediate files ( assembling, preprocessing) in gcc”

Leave a Reply

Your email address will not be published. Required fields are marked *