Ad

VisualStudio *.obj Files Size (513Mb Objs And 534Mb Lib)

- 1 answer

I'm a C++ developer and using Visual Studio 2008. How can I reduce *.obj files size? I've read why are my visual studio .obj files are massive in size compared to the output .exe? but didn't find the answer.

When I build project to a static lib total size of all *.obj files is 513Mb and resulting lib is 534Mb. Each obj file is 1-13Mb. Debug exe file is 11Mb. Link-time code generation (/Gm) is turned off.

Thanks in advance.

Ad

Answer

Object files tend to get large because of large amounts of duplicated code and symbols placed into multiple object files. Typically this is caused by copies of inline functions and instantiated template code.

The resulting executable is much, much smaller than the sum of all the object files as duplicated weak symbols and unreferenced symbols can be discarded.

Personally, I wouldn't worry about object file size, but if you want to reduce it then minimize the use of inline functions and consider keeping template code in separate files from the other code and using explicit instantiation to generate the required instantiations in only one place.

Example

Suppose that you have this in a header file.

template< class T >
inline void F( T* t )
{
    // ... some code
}

If you use F for a handful of types, then every translation unit will generate copies of the function for the types that it uses.

If you replace it with just a declaration:

template< class T > void F( T* t );

then you can generate just the instantations that you need in a separate source file:

template< class T > void F( T* t )
{
    // definition
}

template void F< int >( int* );
template void F< char >( char* );
template void F< MyType >( MyType* );

Of course, you now have to manual manage which types you need a template instantiation for.

Ad
source: stackoverflow.com
Ad