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

标签 c gcc compilation compiler-errors compiler-warnings

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

是否可以在 gcc 中编译文件而不生成任何输出文件?

我知道还有其他方法可以实现这一点,但我很好奇是否有一个标志专门用于检查源代码以查找错误/警告。

最佳答案

您可能喜欢 -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 +++

与使用 -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 +++

关于在 GCC 中编译而不生成输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26738082/

相关文章:

C:这个宏是什么意思?

java - Groovyc *.groovy 奇怪的行为

c++ - 在msys下构建boost,找不到mingw.jam

c - 在 C 编程中存储日志/错误消息

使用虚拟节点连接 2 个单链表

C 数学库 - float 计算错误?

c - 将指针转换为 int64_t 时丢失了最重要的 32 位

c++ - SublimeLinter 无法检查 C 和 C++

c++ - Eclipse - 启动命令 : gdb --version 时出错

C++编译过程: Place for templates