hda 2017
hda 2017

Reputation: 59

Curious static linking behavior

I've been playing around with a finance library called QuantLib (http://quantlib.org/index.shtml) which I've been trying to statically link into one of my Visual C++ programs.

Now, upon initially seeing the size of the static Quantlib library provided (> 400 megs), I was a bit alarmed, as I expected a huge executable file, yet somehow the exe remains small, almost as if the build was dynamically loading only the relevant parts of the library.

Can you advise if there are any Windows programming techniques that might allow this type of "sparse" linking behavior against a huge static library?

Upvotes: 2

Views: 209

Answers (4)

David Heffernan
David Heffernan

Reputation: 613451

The linker simply includes the functions that are needed by the host executable. The functions that are not needed are not included. This is pretty standard behaviour of linkers when linking static libraries.

Upvotes: 2

nos
nos

Reputation: 229274

Normally only the relevant parts of the library are pulled in, not the entire library.

Often this is done on the granularity of individual object files, i.e. a static library is often just a collection of object files from the build of the library, and if your code depends on something in a an object file, the entire object file gets pulled in. Some linkers might be able to separate out individual functions as well.

The library could also contain debug information that got stripped out in your executable.

Upvotes: 0

CB Bailey
CB Bailey

Reputation: 792897

That's just how static libraries work on most platforms. Static libraries are basic archive files consisting of object files. At link time, when you tell the linker to use a static library the linker will choose those object files that help it to resolve undefined symbols in the program and any object files already selected from the static library.

Upvotes: 1

James M
James M

Reputation: 16728

Well, if your project contains code that isn't referenced from anywhere (and isn't going to be exported), it gets removed when building the executable.

It's exactly the same with a static library - if you don't use a function contained in the library, it won't be included.

Upvotes: 3

Related Questions