Compile without generating output file in GCC

試著忘記壹切 提交于 2019-11-30 04:14:00

问题


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


回答1:


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 +++


来源:https://stackoverflow.com/questions/26738082/compile-without-generating-output-file-in-gcc

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