Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make gcc skip preprocessing?

Tags:

c

clang

I want to do some testing and want the input file to be passed to the c compiler directly, not being preproecssed. How can I do that? Thanks!

like image 584
noinput Avatar asked May 05 '26 18:05

noinput


1 Answers

If the source file that you do not want to be preprocessed is a C source file foo.c then rename it to foo.i. If it is a C++ source file then rename it to foo.ii. This will cause the compiler to skip preprocessing. Just compile as usual, e.g.

gcc -c -o foo.o foo.i

However, if you try to compile a source file that contains preprocessing directives ('#'-lines) without preprocessing it then the compilation will simply fail, like:

$ cat foo.c
#include <stdio.h>

int main()
{
    puts("Hello world");
    return 0;
}

$ cp foo.c foo.i
$ gcc -c -o foo.o foo.i
foo.i:1:1: error: stray ‘#’ in program
 #include <stdio.h>
 ^
foo.i:1:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
 #include <stdio.h>
      ^

Possibly what you want to do is compile a source file that you have already preprocessed without preprocessing it again. In that case, first preprocess foo.c, writing the output to foo.i, then compile foo.i:

$ cpp foo.c > foo.i
$ gcc -c -o foo.o foo.i
$ gcc -o foo foo.o
$ ./foo
Hello world

Presumably you would do something with, or to, foo.i between creating it and compiling it; otherwise you might as well just compile foo.c.

gcc in my machine is a symbolic link to clang

OS X? No matter: you can substitute clang for gcc throughout the above.

See also

like image 154
Mike Kinghan Avatar answered May 08 '26 11:05

Mike Kinghan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!