Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / May 2007



Tip: Looking for answers? Try searching our database.

C extern variables unresolved within static libraries (static linking)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aiooua - 29 May 2007 12:12 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.