tags:

views:

529

answers:

2

I'm pretty new to the C++ build flow, and I'm thinking of switching to use partial linking for my libraries instead of creating ar archives. I'm hoping to reduce link time in an inevitable final compilation step that I have, and I figure partial linking some libraries once could save me time over linking everything in that final step.

Is what I'm describing possible? I figure it should be something along the lines ld -Ur -o mylib.o [components]. Are there important build considerations that I'm not taking into account?

+5  A: 

You lose an important effect of having the object files in an ar archive, which is that only the referenced objects will be linked in.

If you have both foo.o with the symbol foo and bar.o with the symbol bar in an ar archive, and only reference the foo symbol, only foo.o would be linked in. If you instead do a partial link, the contents of both will end up in the executable, even if bar is never referenced anywhere.

You could also try a faster linker, like gold.

CesarB
So basically executable bloat can be expected as a downside of the faster linkage, right?
cdleary
Only if you do have objects in it that would not otherwise be linked in. If your library is part of your project, it's quite possible that you use all of it, and thus would have no extra bloat. The Linux kernel build system uses ld -r a lot, but passes it only the .o files it knows will be used.
CesarB
A: 

The other time that you want to use ld -r to do a partial link is if you have some object files that contain static initialisation that are required to provide certain functionality but that is never called directly by any of the code.

If you'd like I can write up a document that showcases how this works and why it works!

(see run time C++ class registration for example, it uses a static register function that registers itself in a registry, which can then be used to create and return a Base* with polymorphic behaviour since it created a new object that was inherited from Base. See http://stackoverflow.com/questions/77817/c-runtime-knowledge-of-classes/77911#77911 which is an answer to a question I asked.)

X-Istence