gcc static library binding failure

Programming, for all ages and all languages.
Post Reply
Rival
Posts: 11
Joined: Sat Feb 05, 2011 6:05 am
Location: I gotta go

gcc static library binding failure

Post by Rival »

First off, some specifics:

Gcc: cross compiled under Cygwin to support plain binary
Environment: freestanding/ Part of OS development
parameters to gcc: -nostdlib -nostartfiles -nostdinc

So here goes the problem:
Now that I've a minimal C library implemented I decided to create a static library. For all function call appearing in the library, I've corresponding header files. All functions in a particular header file (and corresponding .c file before creating the library) are standalone and hence make calls only to the functions available within that file only. No external calls. I compiled all the source files with -c parameter and created a static library with:

ar rcs libstatic.a *.o

This generates libstatic.a. But now whenever I try to link this library with my source files using ld, ld throws and error text+xxx: Undefined reference to xxx. This happens for all calls to function that are available in the static library. Note that, I've #include the corresponding header files that have the function definition.

That being said, if I link the object files instead of the library, everything goes well; no errors, no warnings. So I presume somehow the library is not being created as I've expected!

So, if anyone has any insight on this matter, please respond. Thank you in advance!
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: gcc static library binding failure

Post by iansjack »

What is the exact command you are using for the link?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: gcc static library binding failure

Post by Combuster »

ar
Start with actually using the cross-compiler, thank you.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Rival
Posts: 11
Joined: Sat Feb 05, 2011 6:05 am
Location: I gotta go

Re: gcc static library binding failure

Post by Rival »

Combuster wrote:
ar
Start with actually using the cross-compiler, thank you.
The archiver is a part of cross compiled binaries. Literally, it should be able to archive the cross compiled object files.
iansjack wrote:What is the exact command you are using for the link?
ld -T xxx.ld -o xxx xx1.o xx2.o xx3.o xxx.o --library=static

Anyway, I fixed the problem by specifying library search path using -L switch to ld. I assumed that the default search path would be the current directory. Nevertheless, it is strange that neither ld nor gcc complains on missing library even if it is unable to find the library file I'm specifying.

Still, thank you for your responses.
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: gcc static library binding failure

Post by iansjack »

Rival wrote:Nevertheless, it is strange that neither ld nor gcc complains on missing library even if it is unable to find the library file I'm specifying.
I think this is consistent with the behaviour of Unix elsewhere. Bear in mind that "--library=static" isn't saying "link library libstatic into the executable"; rather it is saying "include library libstatic in the list of places to search for unresolved symbols". If you include a non-existing directory name in the PATH environment variable, bash doesn't produce a warning about this; so ld doesn't warn you if you specify a (possible) search location that doesn't exist. Again, it is consistent that "." isn't, by default, included in the search path.
Post Reply