Compile without generating output file in GCC

后端 未结 1 1502
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 21:06

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

Is it possible to compile files in gcc

相关标签:
1条回答
  • 2021-01-04 21:32

    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 +++
    
    0 讨论(0)
提交回复
热议问题