Yeah, I know many people asked that question before, but I still can't understand the problem in my case
是的,我知道很多人之前都问过这个问题,但我仍然无法理解我的问题
I have 2 libs, let's say liba & libb. libb uses liba but is compiled in .a so it should link at compile time. I have the following GCC command:
我有2个库,比方说liba和libb。 libb使用liba但是在.a中编译,所以它应该在编译时链接。我有以下GCC命令:
gcc -o my_program obj/mymain.o obj/myutils.o liba/liba.a libb/libb.a -Iinclude -Iliba -Ilibb
But GCC is returning me a lot of "Undefined reference to ..." from libb functions to liba functions.
但是GCC从libb函数向liba函数返回了很多“Undefined reference to ...”。
What is happening? What should I do?
发生什么事?我该怎么办?
Thank you
1 个解决方案
#1
3
The evaluation of commands on a link
compile command is very important.
对链接编译命令的命令评估非常重要。
When the compiler sees .o files, they get added to the target binary automatically, so all .o files are present. That leaves a list of undefined entities which need to be found.
当编译器看到.o文件时,它们会自动添加到目标二进制文件中,因此所有.o文件都存在。这留下了需要找到的未定义实体的列表。
The next stage is to look through the libraries. Each library is searched, and the .o elements of each library which fulfills an undefined reference is added to the target binary. That always resolves some issues. However, it may also have further requirements. So adding part of a library may add to the required elements to be satisfied.
下一步是浏览图书馆。搜索每个库,并将每个库中满足未定义引用的.o元素添加到目标二进制文件中。这总能解决一些问题。但是,它可能还有其他要求。因此,添加库的一部分可能会增加要满足的所需元素。
When a library requires another library, it needs to be specified after something which required it, and before the libraries which satisfy its requirements.
当库需要另一个库时,需要在需要它之后,以及满足其要求的库之前指定它。
There is a chance if the .o files also require the same parts of a library, this issue can crop up when code is deleted from a .o (removing the mechanism which pulls in the library part).
如果.o文件也需要库的相同部分,则有可能在从.o删除代码时删除此问题(删除引入库部分的机制)。
#1
3
The evaluation of commands on a link
compile command is very important.
对链接编译命令的命令评估非常重要。
When the compiler sees .o files, they get added to the target binary automatically, so all .o files are present. That leaves a list of undefined entities which need to be found.
当编译器看到.o文件时,它们会自动添加到目标二进制文件中,因此所有.o文件都存在。这留下了需要找到的未定义实体的列表。
The next stage is to look through the libraries. Each library is searched, and the .o elements of each library which fulfills an undefined reference is added to the target binary. That always resolves some issues. However, it may also have further requirements. So adding part of a library may add to the required elements to be satisfied.
下一步是浏览图书馆。搜索每个库,并将每个库中满足未定义引用的.o元素添加到目标二进制文件中。这总能解决一些问题。但是,它可能还有其他要求。因此,添加库的一部分可能会增加要满足的所需元素。
When a library requires another library, it needs to be specified after something which required it, and before the libraries which satisfy its requirements.
当库需要另一个库时,需要在需要它之后,以及满足其要求的库之前指定它。
There is a chance if the .o files also require the same parts of a library, this issue can crop up when code is deleted from a .o (removing the mechanism which pulls in the library part).
如果.o文件也需要库的相同部分,则有可能在从.o删除代码时删除此问题(删除引入库部分的机制)。