StevieG
StevieG

Reputation: 8709

Huge static library file when compiled using VS2005, normal size when compiled using gcc

I'm compiling a static library that contains mainly templated classes. When this is compiled using gcc, the resulting .a file is around about the 40Mb mark. This is pretty big, but not entirely unexpected due to the amount of templating going on. However, when I compile the same code using VS2005, the resulting .lib file is coming in at (wait for it!) 575Mb..

Now, before I get burned, I have seen: How can I get my very large program to link? and this is useful for understanding that templates are potentially making the libs large, but I'm struggling to understand why the outputs from the two compilers are so different in size?

VS options are: (Debug)

/Od /D "WIN32" /D "_DEBUG" /D "_LIB" /D "_WIN32_WINNT=0x0500" /D "_MBCS" /Gm /EHsc /RTC1 /MDd /W4 /nologo /c /Wp64 /Zi /TP /errorReport:prompt

(Release)

/O2 /D "WIN32" /D "NDEBUG" /D "_LIB" /D "_WIN32_WINNT=0x0500" /D "_MBCS" /FD /EHsc /MD /W4 /nologo /c /Wp64 /Zi /TP /errorReport:prompt

Any comments or pointers are much appreciated..

Upvotes: 5

Views: 1562

Answers (1)

Alan Stokes
Alan Stokes

Reputation: 18964

A debug build disables inlining and also the linker options that discard duplicate code, so you get lots of copies of every template and inline function.

You can enable it with /OPT:REF /OPT:ICF in your linker options. But it should be there by default in a release build.

Unfortunately I think that only helps with your final executable, not the intermediate library.

You might be able to save some space by explicitly instantiating the template instances you need in one .cpp, and using extern template to prevent automatic instantiation when compiling other source files.

Upvotes: 3

Related Questions