gpt4 book ai didi

c - 预处理器在系统编译流程中做什么?

转载 作者:行者123 更新时间:2023-11-30 19:10:42 26 4
gpt4 key购买 nike

我已经开始阅读《计算机系统:程序员的视角(测试版草案)》一书。它解释了从C源代码到可执行程序的系统编译,示例代码在书中:

#include <stdio.h>

int main()
{
printf("hello, world\n");
}

我不明白,为什么预处理器必须将系统头stdio.h的内容作为文本放在源文本上?由于组成 printf 函数的机器代码在链接阶段与组成示例源代码的机器代码合并,因此当 #include 时,到底在源代码文本中插入了什么> 检测到指令?

System compilation picture from the book

难道我都听错了?或者对于所有这些阶段有什么我不知道的吗?

最佳答案

why does the preprocessor has to put the content of the system header stdio.h on the source text as text?

这就是 #include 的全部要点 - 您还希望 #include 做什么?

since the machine code that composes printf function is merged with machine code that composes the example source code at the linking phase,

#include 与机器代码无关。它仅适用于源代码。

what is exactly inserted at the source code text when the #include directive is detected?

stdio.h 的内容。如果您使用的是 Linux 系统,则可以在/usr/include/stdio.h 中找到该文件。如果您使用的是 Windows,您可能需要搜索 Program Files。

请注意,printf 在 stdio.h 中定义;它只是被声明。

如果您想确切知道预处理后源文件的样子,并且您使用的是 GCC,则可以运行 gcc -E nameofyoursourcefile.c

Did I get it all wrong? or is there something I do not know about all those phases?

看来您认为 printf 的源代码位于 stdio.h 中。事实并非如此。

关于c - 预处理器在系统编译流程中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40985018/

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