【问题标题】:Compile without generating output file in GCC在 GCC 中编译而不生成输出文件
【发布时间】:2014-12-31 12:23:46
【问题描述】:

$ gcc -c somefile.c不链接编译,生成对应的somefile.o。

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

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

【问题讨论】:

  • gcc -o /dev/null somefile.c 呢?
  • 甚至gcc -S -o /dev/null somefile.c ?
  • 更好用gcc -Wall -Wextra -c somefile.c -o /dev/null;你真的想要警告!
  • @BasileStarynkevitch 啊,但我愿意!只是不想弄乱这个问题。

标签: c gcc compilation compiler-errors compiler-warnings


【解决方案1】:

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

【讨论】:

  • 这是我一直在寻找的,虽然它抑制了一些输出;例如-Wunused-function.
  • 是的; gcc -Wall -Wextra -O -c somefile.c -o /dev/null 更可取,因为优化过程会发出一些警告。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 2021-08-23
相关资源
最近更新 更多