问题描述
例如
#include <iostream>
int add(int x, int y);
int main()
{
cout << add(5, 5) << endl;
}
这将编译但不链接。我理解这个问题,我只是不明白为什么它编译罚款,但不链接。
This would compile but not link. I understand the problem, I just don't understand why it compiles fine but doesn't link.
推荐答案
输入到C或C ++编译器一个的 - 或多或少的一个来源$ C $ C文件。一旦编译器与一个源$ C $ C完成后,它已经完成了它的任务。
The input to a C or C++ compiler is one translation unit - more or less one source code file. Once the compiler is finished with that one source code, it has done its job.
如果你调用/使用符号,如函数,这是不是翻译单元的一部分,编译器假定它的定义在别处。
If you call/use a symbol, such as a function, which is not part of that translation unit, the compiler assumes it's defined somewhere else.
之后,你链接在一起所有的目标文件,并可能要使用的库,所有引用绑在一起 - 这是只有在这一点上,拉在一起的目的是为了建立一个可执行的一切的时候,有可能知道的东西丢失了。
Later on, you link together all the object files and possibly the libraries you want to use, all references are tied together - it's only at this point, when pulling together everything that's supposed to create an executable, one can know that something is missing.
这篇关于为什么,当你没有定义你得到一个链接错误,而不是一个编译器功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!