我正在编译一个主要包含模板类的静态库。当使用 gcc 编译时,生成的 .a 文件大约有 40Mb 左右。这是相当大的,但由于正在进行的模板量,这并不完全出乎意料。然而,当我使用 VS2005 编译相同的代码时,生成的 .lib 文件大小为(等待!)575Mb。
现在,在我被烧毁之前,我看到了: 我怎样才能链接我的非常大的程序?这对于理解这一点很有用模板可能会使库变大,但我很难理解为什么两个编译器的输出大小如此不同?
VS 选项有:
(调试)
/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
(发布)
/O2 /D "WIN32" /D "NDEBUG" /D "_LIB" /D "_WIN32_WINNT=0x0500" /D "_MBCS" /FD /EHsc /MD /W4 /nologo /c /Wp64 /Zi /TP /errorReport:prompt
非常感谢任何评论或指针。
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..
发布评论
评论(1)
调试版本会禁用内联以及丢弃重复代码的链接器选项,因此您会获得每个模板和内联函数的大量副本。
您可以在链接器选项中使用
/OPT:REF /OPT:ICF
启用它。但它应该默认存在于发布版本中。不幸的是,我认为这只对您的最终可执行文件有帮助,对中间库没有帮助。
您可以通过在一个 .cpp 中显式实例化所需的模板实例并使用 extern template 防止编译其他源文件时自动实例化。
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.