Skip to main content
gamedev.net
🔒 Locked

Static lib size

Started by Cluq Jan 15, 2008 at 4:35 AM 11 replies 1.2k views
Original Post
Cluq
Cluq
Hi I am creating a static (game) library in VC++. Up until now I have been using precompiled headers, e.g. all .cpp files included the precompiled header and all .h files included nothing. I didn't like the approach and decided to get rid of the precompiled headers. All .h and .cpp files were then updated with their appropriate includes and it all worked fine. But then I looked at the file size of my static library. It had grown from 5 MB to 22 MB! I first thought that I might have been including some other static library that got built into my library, but I haven't included any libraries. Can anyone enlighten me in what happened?! /Cluq.
Evil Steve
Evil Steve
I'd assume that the precompiled headers cause the compiler to generate smaller code for them, which I guess makes sense, although I wouldn't expect such a vast difference.

Is your compiled exe (I assume you have a test exe) the same size as before?
Cluq
Cluq
I do have a test.exe, but some refactoring have made it "uncompilable" at the moment..

Could anyone explain why the compiler generates smaller code using precompiled headers? - or perhaps throw a link...
oler1s
oler1s
Quote:
But then I looked at the file size of my static library. It had grown from 5 MB to 22 MB!
Sorry, but this doesn’t compute. You may have accidentally set something to become a debug build or linked the wrong runtime, I don’t know.

A precompiled header doesn’t change code or anything. It’s about starting off from an intermediate step instead. Instead of working through the headers, the compiler has a file format it can automatically understand.
Antheus
Antheus
Better question is - why does it matter?

Static libs will not be included verbatim, so whatever their size may be is irrelevant, compiled exe will be just as big as it needs to be.
Marineio
Marineio
The compiler (If you're using msvc++) will optimise it when you compile.
Cluq
Cluq
Quote:
Original post by oler1s
Sorry, but this doesn’t compute. You may have accidentally set something to become a debug build or linked the wrong runtime, I don’t know.


I thought so to, but I didn't change anything - I have run through the project settings twice :)

Quote:
A precompiled header doesn’t change code or anything.


That was also my belief, which made me ask the question in the first place.

Quote:
Original post by Antheus
Better question is - why does it matter?


If I have to distribute my library, I would prefer to distribute 5 MB instead of 22 MB.

Quote:
Original post by Marineio
The compiler (If you're using msvc++) will optimise it when you compile.


But all I did was to change from precompiled headers, so why did it not optimize the library?

It is still puzzling to me. I will try and recreate the situation again when I get the time.
Antheus
Antheus
Your link-time code generation option changed (/LTCG).

With that enabled, linker is capable of doing further optimizations, such as inlining functions across library boundaries.

This means, that lib contains *everything*, even if never used. If you compile library without that, then it'll contain only things it knows will be used and are accessible by providing only public symbols.

The result is small library, but linking against such library will result in potentially slower code.

DrEvil
DrEvil
It's not really a good idea to release compiled static libraries anyways, as you'd have to distribute different libraries for each compiler, each code generation setting, etc(If it's a C++ lib). Let the user compile it. The static lib size is meaningless.
Cluq
Cluq
Quote:
Original post by Antheus
Your link-time code generation option changed (/LTCG).

With that enabled, linker is capable of doing further optimizations, such as inlining functions across library boundaries.

This means, that lib contains *everything*, even if never used. If you compile library without that, then it'll contain only things it knows will be used and are accessible by providing only public symbols.

The result is small library, but linking against such library will result in potentially slower code.


Thank you very much for that reply - that was what I was looking for :)

Quote:
Original post by DrEvil
It's not really a good idea to release compiled static libraries anyways, as you'd have to distribute different libraries for each compiler, each code generation setting, etc(If it's a C++ lib). Let the user compile it. The static lib size is meaningless.


Well, if you don't want to distribute the source, then what do you do? Distribute a .dll and the header files?

I thought static libs were a convenience to people, making them able to link my code into their exe, instead of linking to a .dll. Am I wrong here?
Evil Steve
Evil Steve
Quote:
Original post by Cluq
Quote:
Original post by DrEvil
It's not really a good idea to release compiled static libraries anyways, as you'd have to distribute different libraries for each compiler, each code generation setting, etc(If it's a C++ lib). Let the user compile it. The static lib size is meaningless.


Well, if you don't want to distribute the source, then what do you do? Distribute a .dll and the header files?

I thought static libs were a convenience to people, making them able to link my code into their exe, instead of linking to a .dll. Am I wrong here?
It's not really much of a convenience though. A static lib contains the code in a similar way to a DLL, except a static lib's code is built into the exe when it's compiled, and a DLL is linked when the exe is run (Hence the term Dynamic Link Library). That means another file kicking about with the app, which some people don't like.

You'll see quite a few posts on this forum from people trying to use libraries like Lua or Ogg Vorbis which are compiled as static libs under VC6, but cause all sorts of linker errors when trying to link them with VC2005 / VC2008.

Generally, I compile my libraries (Like a PAK file reader I have) as static libs, but also have the source available. That way if someone wants a static lib, they can either grab that or compile from source.
Antheus
Antheus
Quote:
Original post by Cluq

Well, if you don't want to distribute the source, then what do you do? Distribute a .dll and the header files?

I thought static libs were a convenience to people, making them able to link my code into their exe, instead of linking to a .dll. Am I wrong here?


If I have the slightest possible chance, I will not touch a third-party library that doesn't come with source.

The Windows libraries are pain enough to deal with as a black box, I don't need a piece of software written by a developer with a fraction of QA that MS has (and I know the reputation MS has with regard to that).

Source, or I'm outta here. Or 24/7 free support (as in, not billed per hour).

There are cases when this isn't possible, but outside of a few very specialized examples, there are always source-based alternatives.

And if I'm paying a fair sum for a certain library, then you better give me everything.
Cluq
Cluq
I totally agree - when you pay for something, you want the whole nine yards.

But if you get it for free, you tend to be more tolerant :-)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.