gpt4 book ai didi

c - 编译 .i 或 .ii 文件时忽略 gcc 标志 -include

转载 作者:太空宇宙 更新时间:2023-11-04 03:10:43 25 4
gpt4 key购买 nike

我最近发现如果文件结尾是 .i 或 .ii,gcc 会跳过预处理,因此决定试一试。编译一个不包含 stdio.h 的 hello world 程序:

gcc -Wall file.c; # compiles with preprocessor, implicit declaration of puts
gcc -Wall file.i; # compiles without preprocessor, implicit declaration of puts

如果没有预处理器指令,我不能包含 stdio.h,但我记得 gcc 的 -include 标志可用于“强制包含” header 。它导致了以下测试:

gcc -Wall -include stdio.h file.c; # no warnings, "hello world". hooray
gcc -Wall -include stdio.h file.i; # implicit declaration of puts WAIT WHAT?!

我觉得奇怪的是,如果在没有预处理的情况下编译文件,gcc 不包含 stdio.h。更奇怪的是没有发出警告; -include stdio.h 没有明显效果,充其量是 gcc 的错误使用。

为什么它不起作用?

海湾合作委员会版本 6.3.0。

最佳答案

-include列在 Preprocessor Options 下:

3.12 Options Controlling the Preprocessor

These options control the C preprocessor, which is run on each C source file before actual compilation.

[...]

-include <em>file</em>

Process file as if #include "file" appeared as the first line of the primary source file. [...]

但是,对于 .i预处理器永远不会运行的文件,因此该选项无效。

GCC 通常不会对无效的选项发出警告。您还可以运行 gcc -Wall -funsigned-char foo.o ,它甚至不调用编译器。 -Wall-funsigned-char被简单地忽略了。

您可以将编译视为管道:

  1. C 代码 ( .c ) 通过预处理器,产生 ...
  2. 预处理的 C 代码 ( .i ),由编译器处理生成 ...
  3. 汇编程序代码 ( .s ),由汇编程序处理生成 ...
  4. 目标代码 ( .o ),由链接器处理,给你......
  5. 一个可执行文件。

文件名告诉 GCC 从哪个阶段开始。

选项可用于告诉 GCC 在何处停止:

  • -P预处理后停止
  • -S编译后停止
  • -c组装后停止

其他选项被传递到管道中的相应阶段。如果管道的那部分从未运行,则什么也不会发生。

关于c - 编译 .i 或 .ii 文件时忽略 gcc 标志 -include,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56600135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com