Sept
Sept

Reputation: 181

VC++ linker to remove indirect dependencies for static libraries

Consider this scenario (everything in release mode):

a.lib includes f1() and f2().
a.lib is built using /LTCG on VS2015.
f1() is local without any external function calls.
f2() calls fc() from b.lib

b.lib includes fc() and 100s of other functions.
b.lib is built by a 3rd party, probably not VS.

main.exe is built only with main.cpp, using /LTCG on VS2015.
main.exe links to a.lib and b.lib
main.cpp only calls f1()

Now, when I build main.exe, I expect it to include only f1()'s implementation. The executable size is ~10MB.

However, if I comment out f2()'s implementation in a.lib and rebuilt a.lib, then main.exe becomes 200KB. It works exactly the same in both cases.

My concern is both the size of the executable and exposing anything about b.lib (which is not my own library) unnecessarily.

Question: why is the linker not smart enough to not include f2()->fc() definitions? while it is smart enough to not include the rest of b.lib (which is more than 100MB)?

Upvotes: 1

Views: 120

Answers (1)

The linker is dragging in all the statics that fc may be using (like a map of error numbers to strings), and all the functions (potentially) called by fc, and all the functions that the statics use to initialize themselves.

I wouldn't worry about exposing your use of b.lib (providing you have properly licensed it), protecting the IPR in that is the vendor's problem.

Executable size is a genuine concern, but I don't think there is much you can do about it.

Upvotes: 1

Related Questions