简体   繁体   English

linker 如何找到正确的库?

[英]How the linker find the correct library?

I know what preprocessing is, how compiler works, and how linker links the object files我知道预处理是什么,编译器如何工作,以及 linker 如何链接 object 文件

But what I still haven't been able to answer is:但我仍然无法回答的是:

In an IDE like VS在 IDE 像 VS

suppose we have a library called DariushTest.o that implemented a function called print() and have a header file called test.h and we have other libraries that implemented the print() function but we don't include their header files in our code suppose we include the test.h header in our code and we use the print() function in our main function suppose we have a library called DariushTest.o that implemented a function called print() and have a header file called test.h and we have other libraries that implemented the print() function but we don't include their header files in our code假设我们在我们的代码中包含test.h header 并且我们在我们的主要 function 中使用print() function

NOW.现在。 if there are several libraries that have implemented this function.如果有几个库已经实现了这个功能。

after compiling the code, how does the linker find out which library this code is related to and with which file should it link?编译完代码后,linker如何找出这段代码与哪个库相关以及应该链接到哪个文件?

The reason that I ask this question is that the header file doesn't connect to DariushTest.o file and the compiler doesn't care about it.我问这个问题的原因是 header 文件没有连接到 DariushTest.o 文件并且编译器不关心它。

So how linker links the print() function from DariushTest.o not from other libraries?那么 linker 如何链接来自DariushTest.oprint() function 而不是来自其他库? How the linker find the correct library? linker 如何找到正确的库?

So how linker links the print() function from DariushTest.o not from other libraries?那么 linker 如何链接来自 DariushTest.o 的 print() function 而不是来自其他库?

It doesn't.它没有。 If you break the ODR by having the same function name in 2 different compilation units, then the behavior of the program is undefined.如果通过在 2 个不同的编译单元中使用相同的 function 名称来破坏ODR ,则程序的行为是未定义的。 The linker might notice and diagnose the redefinition and might not. linker 可能会注意到并诊断重新定义,但可能不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM