sssa2000
sssa2000

Reputation: 11

How do i make dll smaller with MSVC++ 2010?

i have some huge c++ projects, all of then are compiled with msvc++ 2010. i want the DLL file to be smaller, can anyone give me some inspiration?

Upvotes: 1

Views: 912

Answers (4)

selbie
selbie

Reputation: 104539

  1. Compile as release, not debug.

  2. Link with MSVCRT dynamically instead of statically. This means you likely have to distribute the MSVCRT DLL with your program. Depending on how your program is structured, changing the links of CRT can have unintended side effects.

  3. Remove all the code that isn't needed. Use a profiling or code coverage tool to identify code that doesn't appear to be getting called. You might be able to remove it.

  4. Look at all the corresponding .obj files for each .c or .cpp file. If any one obj file is excessively large relative to the size of the code file, that might be a hint something could be reduced there.

  5. Minimize the use of global instances or global data in your DLL. The binary size will bloat by the number of bytes of global variables declared.

  6. Only export the very minimal number of functions you need to have other EXEs and DLLs import. Run "dumpbin /exports yourfile.dll" to get the list of exported functions. Only export functions that are directly called by the code relying on the DLL. If you are exporting something that no one outside of the DLL is going to call directly, don't export it. The linker will optimize it (and it's dependents) from being used if nothing internal is calling it.

  7. Don't export entire C++ classes. Export simple C wrapper functions if your DLL is C++ code.

Upvotes: 0

user1095108
user1095108

Reputation: 14603

In addition to the other answers, you can use upx to compress the dll, or some other compressor.

http://upx.sourceforge.net/

Upvotes: 1

Crusher
Crusher

Reputation: 64

In addition to the above suggestions, Make sure in Project Properties->C/C++->Favor Size Or Speed, Favor small code (/Os) is selected.

Upvotes: 0

MSalters
MSalters

Reputation: 179859

Compile for release, use Link Time Code Generation (LTCG), remove unused references (OPT:ICF), put the CRT in a DLL. Don't export things from the DLL unless necessary.

Upvotes: 2

Related Questions