Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile without generating output file in GCC

$ gcc -c somefile.c compiles without linking and generates the corresponding somefile.o.

Is it possible to compile files in gcc without generating any output file?

I know there are other ways to achieve this but I'm curious on whether there is a flag just for going through the source code looking for errors/warnings.

like image 592
Daniel Avatar asked Nov 04 '14 14:11

Daniel


1 Answers

You may like the -fsyntax-only option. It does not write anything on disk, just checks that the code is valid.

You can check that it does not write anything on disk with this command:

$ strace -e write -f gcc -fsyntax-only test.c
Process 14033 attached
[pid 14033] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=14033, si_status=0, si_utime=0, si_stime=0} ---
+++ exited with 0 +++

Compare with this other command that uses -c -o /dev/null instead:

rodrigo@P41CCTX5:/tmp$ strace -e write -f gcc -c -o /dev/null test.c
Process 14182 attached
[pid 14182] write(3, "\t.file\t\"a.c\"\n\t.text\n\t.globl\tfoo\n"..., 353) = 353
[pid 14182] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=14182, si_status=0, si_utime=0, si_stime=1} ---
Process 14183 attached
[pid 14183] write(3, "\0a.c\0foo\0", 9) = 9
[pid 14183] write(3, "U\211\345]\303\0GCC: (Ubuntu 4.8.2-19ubunt"..., 42) = 42
[pid 14183] write(3, "\24\0\0\0\0\0\0\0\1zR\0\1|\10\1\33\f\4\4\210\1\0\0\34\0\0\0\34\0\0\0"..., 56) = 56
....
[pid 14183] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=14183, si_status=0, si_utime=0, si_stime=0} ---
+++ exited with 0 +++
like image 128
rodrigo Avatar answered Oct 22 '22 18:10

rodrigo