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

匆匆过客 提交于 2020-05-24 20:31:46

问题


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

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

I know that running gcc -E foo.c stops the compiler after preprocessing; but what is the real world application of creating .i files.

Also is there a way of generating a .i files other than gcc foo.c -E > foo.i?


回答1:


The .i files are also called as "Pure C files". In preprocessing stage

  1. Header files will be included.

  2. Macros will be replaced.

  3. Comments are removed.

  4. Used for conditional compilation. If you look at the .i file you can see these things.

Command to generate .i file is-

gcc -E foo.c -o foo.i



回答2:


A file.i file is:

C source code that should not be preprocessed.

Source: man gcc then search for "\.i".
Detailed Steps: man gcc, then press the / key to search, then type in \.i, then press the Enter key, then press the n key repeatedly until you find it.

What this means is that a .i file is preprocessed source code, so it already contains:

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

...as stated by @Sathish in his answer. You'll also notice a ton of special "comments" added by gcc that now begin with the # character, such as these:

# 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 such as 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

Will produce a main.i file that is about 682 lines long, with the main() function shown above being right at the very end.

How to generate all intermediate files, including the .i files:

My preference is to generate all intermediate files (.i, .o, .s, rather than just the .i file using -E) all at once in a local bin folder in the project like this:

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


来源:https://stackoverflow.com/questions/25137743/where-do-we-use-i-files-and-how-do-we-generate-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!