hello,
i'm having troubles with symbol resolution of variables when linking
to a static library. here's a simple setup that can be used reproduce
the issue:
---------------------------------------------------
~/mactest/tmp>cat one.c
int a;
~/mactest/tmp>cat two.c
#include<stdio.h>
extern int a;
void show(){printf("a is %d\n",a);}
~/mactest/tmp>cat main.c
int main(){show();}
~/mactest/tmp>make main
cc -c -o main.o main.c
cc -c -o one.o one.c
cc -c -o two.o two.c
libtool -o liba.a one.o two.o
cc main.o -L. -la -o main
/usr/bin/ld: Undefined symbols:
_a
collect2: ld returned 1 exit status
make: *** [main] Error 1
---------------------------------------------------
the int 'a' is defined in one.o - and it is accessed inside two.o.
when i club the two object files together into a single static library
and link it with an executable the resolution fails, as seen above.
any clues? the "undefined symbols" error goes when i pass the "-
all_load" option. but i don't want to go that way, because "-all_load"
might load a lot of unwanted symbols.
thanks,
Paul Floyd - 29 May 2007 21:11 GMT
> hello,
>
[quoted text clipped - 29 lines]
> all_load" option. but i don't want to go that way, because "-all_load"
> might load a lot of unwanted symbols.
The problem seems to be the circular dependency that ld can't handle.
You could rearrange the code to avoid it. Even when you can get circular
dependencies to link, they add a lot of work to the link editor, and are
best avoided if you can.
Alternatively, "gcc main.c -L. -la -o main" seems to work.
A bientot
Paul