Friday, November 4, 2011

Creating a C++ project without the C runtime library

So you're creating a very small C/C++ program using Visual C++, but you've noticed your program links to the C runtime library (CRT) even though you don't use the CRT. If your project links to the CRT it could cause problems. For starters your file size will be larger, and maybe a small file size is critical for your application. But the more serious problem is one of dependencies. By linking to the CRT your application is now dependent on the CRT libraries which may or may not be installed on every computer your application runs. You can get around this by statically linking to the CRT multithreaded libraries, but this increases your file size even further.

The better solution is to tell the compiler not to link to the CRT in the first place. To do that make the following changes in the project settings:


  1. C/C++ | Code Generation - Disable "Basic Runtime Checks." Anything that adds /RTCx to the command line must be removed.
  2. C/C++ | Code Generation - Disable "Buffer Security Check," i.e. add /GS- option.
  3. Linker | Input - Change "Ignore All Default Libraries" to Yes.
  4. Linker | Advanced - Set the "Entry Point" to "wmain"
  5. Finally in your code, create the entry point "int __stdcall wmain(void)"


This should allow you to compile and link without the CRT. You can create some incredibly small executables this way. However be aware this is best suited for simple apps. Anything more complex, like apps that link to MFC, this technique won't work as most other libraries and DLLs require the CRT.

No comments:

Post a Comment