链接器如何找到正确的库? [英] How the linker find the correct library?

查看:33
本文介绍了链接器如何找到正确的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道什么是预处理、编译器如何工作以及链接器如何链接目标文件

I know what preprocessing is, how compiler works, and how linker links the object files

但我仍然无法回答的是:

But what I still haven't been able to answer is :

在像 VS 这样的 IDE 中

In an IDE like VS

假设我们有一个名为 DariushTest.o 的库,它实现了一个名为 print() 的函数,并有一个名为 test.h 的头文件我们还有其他实现 print() 函数的库但我们没有在我们的代码中包含他们的头文件假设我们在代码中包含 test.h 标头,并在主函数中使用 print() 函数

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

现在!如果有几个库实现了这个功能.

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?

我问这个问题的原因是头文件没有连接到 DariushTest.o 文件,编译器不关心它.

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.

那么链接器如何链接 DariushTest.o 中的 print() 函数而不是其他库?链接器如何找到正确的库?

So how linker links the print() function from DariushTest.o not from other libraries? How the linker find the correct library?

推荐答案

那么链接器如何链接 DariushTest.o 中的 print() 函数而不是其他库?

它没有.如果您通过在 2 个不同的编译单元中使用相同的函数名称来破坏 ODR,那么程序的行为是未定义的.链接器可能会注意到并诊断重定义,也可能不会.

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. The linker might notice and diagnose the redefinition and might not.

这篇关于链接器如何找到正确的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆