Page 1 of 1

bug in ld? (or what?)

Posted: Thu Apr 04, 2002 6:11 am
by The_Legend
Hi,

I'm having a problem with ld.

I have my math functions compiled and packed into an archive math.a and use them in functions from memory.a which are used by main.o.

My problem: ld doesn't include the routines from math.a and complains that they are missing, because it then finds references to them in memory.a.

Is this a bug or what?

Thanks,
The Legend

Re: bug in ld? (or what?)

Posted: Thu Apr 04, 2002 6:55 am
by Dave_Hunt
Two things:

1. You may need to do a ranlib on the libraries.

2. Make sure you specify them in the right order:

   ld <options> main.o memory.a math.a

Good luck!

Re: bug in ld? (or what?)

Posted: Thu Apr 04, 2002 5:28 pm
by Chris Giese
>My problem: ld doesn't include the routines from math.a and
>complains that they are missing, because it then finds
>references to them in memory.a

I assume you're not using the functions in memory.a that
use the math code...?

Did you build memory.a from a single C file containing
all of your functions? The "quantum" (smallest unit) of
linkage is the module, and module = file for C.
If you link against one function in a file, you get the
code for all other functions in that file, whether you use
them or not.

Maybe you can take the functions in memory.a that need the
math code and move them to a one or more separate files.

Re: bug in ld? (or what?)

Posted: Thu Apr 04, 2002 10:08 pm
by The_Legend
>My problem: ld doesn't include the routines from math.a and
>complains that they are missing, because it then finds
>references to them in memory.a

I assume you're not using the functions in memory.a that
use the math code...?

Did you build memory.a from a single C file containing
all of your functions? The "quantum" (smallest unit) of
linkage is the module, and module = file for C.
If you link against one function in a file, you get the
code for all other functions in that file, whether you use
them or not.

Maybe you can take the functions in memory.a that need the
math code and move them to a one or more separate files.
I'm using the memory functions which access the functions in math.a, if I don't use them ld thinks everything is fine as the math functions are currently not used anywhere else (currently!).

I'll see what ranlib can do and if changing the order helps.

Re: bug in ld? (or what?)

Posted: Fri Apr 05, 2002 1:07 am
by The_Legend
Reversing the order has helped, thanks!

Still I think that ld should behave differently ....

Re:bug in ld? (or what?)

Posted: Sat Apr 06, 2002 4:51 pm
by Dave_Hunt
ld is a single-pass linker. As it finds references to undefined lables in a module, it saves them. As those lables are defined, all places that reference them are updated. If it finds a lable that hasn't been referenced yet in any previous module, it will discard it (unless it is referenced in the same module where it's defined).

This is usually how unix linkers work and is not specific to gnu's ld.

At any rate, glad I was able to help!

Re:bug in ld? (or what?)

Posted: Sun Apr 07, 2002 3:25 am
by The_Legend
And why do the unix linkers still work like that?
I think they should change this.