Page 1 of 1

gcc static library binding failure

Posted: Tue Mar 31, 2015 6:32 am
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!

Re: gcc static library binding failure

Posted: Tue Mar 31, 2015 8:11 am
by iansjack
What is the exact command you are using for the link?

Re: gcc static library binding failure

Posted: Tue Mar 31, 2015 9:06 am
by Combuster
ar
Start with actually using the cross-compiler, thank you.

Re: gcc static library binding failure

Posted: Wed Apr 01, 2015 12:31 am
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.

Re: gcc static library binding failure

Posted: Wed Apr 01, 2015 1:53 am
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.