13

$ 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.

4
  • 7
    What about gcc -o /dev/null somefile.c? Commented Nov 4, 2014 at 14:50
  • 3
    Or even gcc -S -o /dev/null somefile.c ? Commented Nov 4, 2014 at 14:53
  • 2
    Better use gcc -Wall -Wextra -c somefile.c -o /dev/null; you really want the warnings! Commented Nov 4, 2014 at 15:08
  • @BasileStarynkevitch Ah, but I do! Just didn't want to clutter the question. Commented Nov 4, 2014 at 15:23

1 Answer 1

21

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 +++
Sign up to request clarification or add additional context in comments.

3 Comments

This is what I was looking for, although it suppresses some of the output; e.g. -Wunused-function.
Yes; gcc -Wall -Wextra -O -c somefile.c -o /dev/null is preferable because some warnings are emitted by optimization passes.
@BasileStarynkevitch Are you sure GCC doesn't do the optimization with this flag? stackoverflow.com/a/42940607/3052438

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.