RE: Transitive Linking fails

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Erik,

> Well, the linking itself was now fine, but I've got new problems in parts of the software that do not use parts of the library.

That sounds odd and unexpected to me.

> When I link the .o files directly into the shared object, then I have no problem, the software runs fine then.

> I've built the lib using
> $(AR) rcs libx.a obj1.o obj2.o
> where $(AR) put out "ar"

That looks good.

> As far as I understood it these two commands should result in the same file:
> $(LD) (shared object options) libshared.so libx.a obj5.o obj6.o
> and
> $(LD) (shared object options) libshared.so obj1.o obj2.o obj5.o obj6.o
> right or not?

No, not right.

For the libx.a file, the linker will only pull over the items from the libx.a that are associated with unresolved symbols.

Since the files are processed in order (left-to-right), as specified on the command-line, the libx.a is processed first.

When the libx.a is processed, at that time there are no unresolved symbols, so nothing will be copied out of the libx.a archive.

> If not what must be done to make these two lines behaviour equivalent?

Try this:

$(LD) (shared object options) libshared.so obj5.o obj6.o libx.a

However...

If you really, really, really want everything included from the libx.a into libshared.so:

$(LD) (shared object options) libshared.so obj5.o obj6.o --whole-archive libx.a --no-whole-archive

However, I discourage that approach.  Instead I suggest you extract out all the .o files from the libx.a, and put them on the link line explicitly:

$(AR) x libx.a obj1.o obj2.o
$(LD) (shared object options) libshared.so obj5.o obj6.o obj1.o obj2.o

> or must I take -static -lx?

Even if you use -static -lx, the order is still important and must appear after your object files (*.o).

Also, you need to "undo" the static state after your library parameters, otherwise the static state is still in effect for any implicit libraries.  (In such a situation people might say, "Why does my libshared.so go from 17 KB to 200 MB with the -Bstatic directive?")

$(LD) (shared object options) libshared.so obj5.o obj6.o -Bstatic -lx -Bdynamic

Sincerely,
--Eljay


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux