Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to function of another lib

Tags:

c

gcc

linker

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:

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.

What is happening? What should I do?

Thank you

like image 365
Alexandre Germain Avatar asked Sep 11 '25 11:09

Alexandre Germain


1 Answers

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.

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.

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).

like image 94
mksteve Avatar answered Sep 14 '25 01:09

mksteve