1

I wrote a very small utility program in C++. I chose C++ for two reasons: to keep .exe size as small as possible and to ensure zero framework dependencies. I just want this to execute as fast as possible with 100% compatibility.

Unfortunately a user has now received an error saying they need MSVCP140.DLL to run the program. I know this is one of the VC++ redistributable packages, but I'm surprised to learn that such a simple console program requires something that's not a default part of Windows.

Is it possible to write dependency-free Windows code? How do I generate a 100% Windows-native .exe file?

From what I've read this may be related to either or both of MFC or ATL and that a solution can be to include them in your release. Is it possible to just turn them off instead? I don't need or want either. (Or maybe MFC is required under the hood for the console window itself?)

Mostly I'm just surprised that it almost seems like there is no such thing as a program that can run completely independently. How can I generate a small program that is completely framework independent?

Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
  • very possible. not use c++ crt libs. if you can. – RbMm May 20 '20 at 20:30
  • @MichaelChourdakis - and as result even helloword will be near 100kb size – RbMm May 20 '20 at 20:31
  • [How do I make a fully statically linked .exe with Visual Studio Express 2005?](https://stackoverflow.com/questions/37398/how-do-i-make-a-fully-statically-linked-exe-with-visual-studio-express-2005) probably still applies. – user4581301 May 20 '20 at 20:32
  • Or maybe it doesn't. In order to have no dependencies you literally have to have no dependencies. You can't use anything from the C or C++ standard libraries. Do-able, but a hard path. – user4581301 May 20 '20 at 20:38
  • @user4581301 doesn't the link you gave tell you how to link the standard libraries too? – Mark Ransom May 20 '20 at 20:47
  • 2
    "*and as result even helloword will be near 100kb size*" - well, you could always write your app in assembly, then an entire complex program can be that very small ;-) (That is what [grc.com does with its software](https://www.grc.com/smgassembly.htm)) – Remy Lebeau May 20 '20 at 20:51
  • @RemyLebeau - why asm ? we can write and on *c++* but still have small code without no crt dependencies. – RbMm May 20 '20 at 20:58
  • @RbMm it was a *joke*, apparently the `;-)` went right over your head. – Remy Lebeau May 20 '20 at 21:09
  • @MarkRansom I read the question again after recommending the link and a few extra things, like "completely framework independent[,]" jumped out at me. The questioner's not getting statically linked Standard library and small. Something has to give. – user4581301 May 20 '20 at 21:25
  • @user4581301 you can certainly make a console program that doesn't require MFC or ATL, thus making it framework independent. The only thing you can't eliminate is the CRT, but that's relatively small. – Mark Ransom May 20 '20 at 21:50

1 Answers1

1

Switch to static non DLL in C++ compiler options for both debug and release builds.

https://learn.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library?view=vs-2019

Remove /MD and use /MT. This increases the executable size, to a very negligible length though.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • isn't this effectively "baking" the framework into your .exe instead of depending on it? how do I remove the dependency altogether? – Dave Cousineau May 20 '20 at 20:33
  • 2
    @DaveCousineau, you cannot. Something has to provide the functionality of the standard libraries. – R Sahu May 20 '20 at 20:35
  • 1
    @Dave even an empty program needs the CRT. – Michael Chourdakis May 20 '20 at 20:35
  • 2
    Just for your information, not using static linking has some advantages, such as: Staying up to date on security fixes is a good reason. Otherwise, you're responsible for rebuilding your application with a fixed CRT and deploying it to your customers. Using a shared CRT should result in lower memory footprint for the system, since most of the DLL's pages can be shared between processes. – Andrey Belykh May 20 '20 at 20:35
  • 1
    These issues are negligible nowadays. It's unlikely that you will have a serious security issue with the CRT and the memory added is small. By contrast, you will have to install (and possibly bundle it with your installer) the runtime to any user that doesn't have it. This increases the complexity of your application without gaining anything significant. As for the security, it's 99.9% that your app itself will induce security issues rather than the current, zillion time tested VC CRT. – Michael Chourdakis May 20 '20 at 20:44
  • 1
    Using static linking has some serious advantages, especially when there is a need for a stable application, such as for machinery. Eliminating external dependencies eliminates the risk of of it turning into an unstable app in the future. Especially on Windows. – Michaël Roy May 20 '20 at 20:53
  • @MichaelChourdakis even big and complex program not need CRT. it can use it, but not need – RbMm May 20 '20 at 21:00
  • this increases my .exe from ~66k to ~370k. maybe I should just write a .bat file. – Dave Cousineau May 21 '20 at 00:05
  • Presumably Windows itself is written in C++ with no CRT, isn't it? Otherwise it would already be in the kernel somewhere. So it must not be strictly impossible (or that assumption is wrong). – Dave Cousineau May 21 '20 at 01:46
  • 1
    You could try using `/ NODEFAULTLIB` If you use /NODEFAULTLIB to build your program without the C run-time library, you may have to also use `/ENTRY` to specify the entry-point function in your program. – Jeaninez - MSFT May 21 '20 at 03:07