在GCC中编译而不生成输出文件

Dan*_*iel 8 c gcc compiler-errors compilation compiler-warnings

$ gcc -c somefile.c编译而不链接并生成相应的somefile.o.

是否可以在gcc不生成任何输出文件的情况下编译文件?

我知道还有其他方法可以实现这一点,但我很好奇是否有一个标志只是为了通过源代码寻找错误/警告.

rod*_*igo 14

你可能会喜欢这个-fsyntax-only选项.它不会在磁盘上写任何内容,只检查代码是否有效.

您可以使用以下命令检查它是否在磁盘上写入任何内容:

$ 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 +++
Run Code Online (Sandbox Code Playgroud)

与使用的其他命令相比-c -o /dev/null:

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 +++
Run Code Online (Sandbox Code Playgroud)

  • 是; `gcc -Wall -Wextra -O -c somefile.c -o/dev/null`是优选的,因为优化传递会发出一些警告. (6认同)