C/C++ 编译器如何在头文件中找到原型的定义?

How does a C/C++ compiler find the definitions of prototypes in header files?(C/C++ 编译器如何在头文件中找到原型的定义?)

本文介绍了C/C++ 编译器如何在头文件中找到原型的定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在头文件中声明一个函数,并将该函数的定义放在其他文件中时,编译器/链接器如何找到定义?它会系统地搜索其路径中的每个文件,还是有更优雅的解决方案?这几天一直困扰着我,一直找不到解释.

When I declare a function in a header file, and put the definition of that function in some other file, how does the compiler/linker find the definition? Does it systematically search every file in its path for it, or is there a more elegant solution? This has been bugging me for the past few days, and I've been unable to find an explanation for it.

推荐答案

编译器不做这个,链接器做.

The compiler doesn't do this, the linker does.

当编译器一次处理一个源文件时,当调用链接器时,它会传递编译器生成的所有目标文件的名称,以及用户希望链接的任何库的名称.因此,链接器完全了解可能包含该定义的文件集,并且只需要查看这些目标文件的符号表.除此之外,它不需要进行任何搜索.

While the compiler works on one source file at a time, when the linker is invoked it is passed the names of all of the object files generated by the compiler, as well as any libraries that the user wishes to have linked in. Therefore, the linker has complete knowledge of the set of files that could potentially contain the definition, and it needs only to look in the symbol tables of those object files. It doesn't need to do any searching beyond that.

例如,假设您有 foo.h 和 foo.c 定义和实现函数 foo(),而 bar.h 和 bar.c 定义和实现 bar().假设 bar 调用 foo 以便 bar.c 包含 foo.h.此编译分为三个步骤:

For example, say you have foo.h and foo.c defining and implementing function foo(), and bar.h and bar.c defining and implementing bar(). Say bar calls foo so that bar.c includes foo.h. There are three steps to this compilation:

gcc -c foo.c
gcc -c bar.c
gcc foo.o bar.o -o program

第一行编译 foo.c,生成 foo.o.第二个编译 bar.c,生成 bar.o.此时,在目标文件 bar.o 中,foo 是一个外部符号.第三行调用链接器,它将 foo.o 和 bar.o 链接到一个名为program"的可执行文件中.当链接器处理 bar.o 时,它会看到未解析的外部符号 foo,因此它会查看所有其他正在链接的目标文件(在本例中为 foo.o)的符号表并找到foo 在 foo.o 中,并完成链接.

The first line compiles foo.c, producing foo.o. The second compiles bar.c, producing bar.o. At this point, in the object file bar.o, foo is an external symbol. The third line invokes the linker, which links together foo.o and bar.o into an executable called "program". When the linker processes bar.o, it sees the unresolved external symbol foo and so it looks in the symbol table of all of the other object files being linked (in this case just foo.o) and finds foo in foo.o, and completes the link.

对于库,这有点复杂,它们出现在命令行上的顺序可能取决于您的链接器,但通常是相同的原则.

With libraries this is a bit more complicated, and the order that they appear on the command line can matter depending on your linker, but it's generally the same principle.

这篇关于C/C++ 编译器如何在头文件中找到原型的定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C/C++ 编译器如何在头文件中找到原型的定义?

基础教程推荐