Where do we use .i files and how do we generate them?

I was looking through the GCC man page, I found the following line:

 file.i
           C source code that should not be preprocessed.

      

I know that startup gcc -E foo.c

stops the compiler after preprocessing; but what is a real file creation application .i

.

Also there is a way to generate files .i

other than gcc foo.c -E > foo.i

?

+6


source to share


2 answers


The files are .i

also called "Pure C files". At the pre-processing stage

  • Header files will be included.

  • Macros will be replaced.

  • Comments are removed.

  • Used for conditional compilation. If you look at the file .i

    , you can see this.



The command to create the file .i

is -

gcc -E foo.c -o foo.i

      

+7


source


File file.i

:

C source code that doesn't need to be preprocessed.

Source: man gcc

then search for " \.i

".
Detailed steps:, man gcc

then press the key /to search, then enter \.i

, then press the key Enter, then press the key several times nuntil you find it.

This means that the file .i

is preprocessed source , so it already contains:

  1. all header files are included
  2. replaced macros
  3. and comments removed

... as @Sathish said in his answer. You will also notice a ton of special "comments" added by gcc that now start with a symbol #

, for example:

# 1 "main.c"  
# 1 "<built-in>"  
# 1 "<command-line>"  
# 1 "/usr/include/stdc-predef.h" 1 3 4  
# 1 "<command-line>" 2  
# 1 "main.c"  
# 44 "main.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  
# 1 "/usr/include/x86_64-linux-gnu/sys/cdefs.h" 1 3 4  

      

Note that a simple program like this:

#include <stdio.h>

int main()
{
    printf("hello world\n");

    return 0;
}

      

Compiled with this:



gcc -Wall -std=c99 -O0 -save-temps=obj main.c -o ./bin/main

      

main.i

the file main.i

is about 682 lines long, with the function main()

shown above at the very end.

How to generate all intermediate files including files .i

:

I prefer to generate all output files ( .i

, .o

, .s

, and not just the file .i

using -E

) at the same time in a local folder bin

of the project:

mkdir bin
gcc -save-temps=obj foo.c -o ./bin/foo

      

Now you have the following in the "foo / bin" directory:

foo     # compiled binary program  
foo.i   # intermediate, preprocessed C file  
foo.o   # object file  
foo.s   # assembly file

      

Run the program, of course, with:

./bin/foo

      

+1


source







All Articles