David
David

Reputation: 29

Cannot understand linker issue

i usually can resolve linker's issue like 'undefined reference' using nm and figuring out that i forgot to add a source file in a makefile but here i can t understand what is happening :

$ make
gcc -I.. -I../../data_structures
-I../../iterator -I../../stack -I../../array_stack 
-Wall -Wextra -Werror  array_collection.o -o array_collection
-L.. -larray_collection
-L../../data_structures -ldyn_array
-L../../iterator -literator
-L../../stack -lstack
-L../../array_stack -larray_stack 
../../array_stack/libarray_stack.a(array_stack_init_stack.o): In function `array_stack_init_stack':
array_stack_init_stack.c:(.text+0x79): undefined reference to `stack_init'
$ nm ../../stack/libstack.a| grep stack_init
auto_stack_init.o:
0000000000000000 T auto_stack_init
0000000000000000 T stack_init

Can you help me please ? It is maybe important to say that the gcc command is aliased to clang on the vm i am using.

Here is the stack_init's code :

void    stack_init(t_stack *stack, void *realisation)
{
    stack->realisation = realisation;
    stack->_release = stack->release;
}

Upvotes: 0

Views: 69

Answers (1)

KamilCuk
KamilCuk

Reputation: 140890

  1. Just use the static library path when compiling with it. So instead of gcc -L../../stack -lstack just use gcc ../../stack/libstack.a.

  2. The order of static libraries matter. If a static library depends on another static library, it has to come before it in the command line arguments. The linker scans the following libraries when it searches symbols, so if you have libarray_stack.a that depends on stack.a, it has to be after it. If they both depend on each other, specify them twice (which is a neat way to handle the problem anyway). Or alternatively with gcc compiler use -Wl,--whole-archive. There are varieus resources on the net that explain why/how it happens, for example this thread.

  3. Take the over 40 years old (but still amazing) make tool back to the shelve and move to cmake or other build systems that handle such problems (and many more) for you.

So try to compile with:

gcc -I.. -I../../data_structures
-I../../iterator -I../../stack -I../../array_stack 
-Wall -Wextra -Werror  array_collection.o -o array_collection
-L.. -larray_collection
-L../../data_structures -ldyn_array
-L../../iterator -literator
-L../../stack -lstack
-L../../array_stack -larray_stack 
-L.. -larray_collection
-L../../data_structures -ldyn_array
-L../../iterator -literator
-L../../stack -lstack
-L../../array_stack -larray_stack 

Upvotes: 1

Related Questions