bug in ld? (or what?)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
The_Legend

bug in ld? (or what?)

Post 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
Dave_Hunt

Re: bug in ld? (or what?)

Post 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!
Chris Giese

Re: bug in ld? (or what?)

Post 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.
The_Legend

Re: bug in ld? (or what?)

Post 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.
The_Legend

Re: bug in ld? (or what?)

Post by The_Legend »

Reversing the order has helped, thanks!

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

Re:bug in ld? (or what?)

Post 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!
The_Legend

Re:bug in ld? (or what?)

Post by The_Legend »

And why do the unix linkers still work like that?
I think they should change this.
Post Reply