Working around LTO issue by wrapping lib

I have a crate that needs to export a staticlib and a lib, but since LTO isn't supported for lib targets, I end up with a big staticlib. This issue is discussed here:

It's mentioned there that this can be worked around by making a wrapper crate that wraps the lib and exports the staticlib, but I'm not really sure what that would look like. Do I need to actually make a new lib.rs file that just re-exports the lib crate? Is there some other magic I should have?

Assuming that the original crate is called my_lib, you can make a new crate with the staticlib crate type which depends on my_lib. This new crate only needs to contains use my_lib; to force my_lib to be linked in. (rustc will likely warn, but you can disable that warning) You don't need to re-export anything as rustc will export all #[no_mangle] functions from a staticlib, even if they originate from dependencies.

1 Like

Note that static libraries are not linked. They're not even real libraries yet. They're an unfinished compilation — a bunch of .o files zipped up in an archive.

You shouldn't worry too much about the size of a static archive. When it's actually linked in a finished product, the linking process will pull in only what is needed.

2 Likes

I follow you, but the libs might enter into some package system and take up storage space and bandwidth before they get linked/windowed.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.