Aristides L
Aristides L

Reputation: 35

How does static linking work with system files?

I'm a beginning C++ programmer and I'm trying to make a fully static program (just one .exe) in Visual Studio. It's gotten me thinking though, because there are some external dependencies that are pulled from the user's computer, like MSVCP.dll. Are those dependencies baked in to the program from the programmer's computer, or are they still left out and pulled from the user?

-Ari

Upvotes: 0

Views: 166

Answers (2)

Miles Budnek
Miles Budnek

Reputation: 30879

The MS Visual C++ compiler gives you the option to choose what version of the MS Visual C/C++ Runtime library you want to link to. The choices are:

  • /MT - Statically link to the non-debug multithreaded version of the CRT. This has the benefit of not requiring you to ship the CRT DLLs along with your application, which may be required for it to run on some systems otherwise. The downside is that the CRT code used by your application has to be baked into your executable, increasing its size. This is compounded if your application has multiple executables and DLLs, since a lot of the same CRT code will have to be baked into each of them.
  • /MTd - Statically link the debug multithreaded version of the CRT. Same as above, but the added debugging info will make it run slower and be even bigger. Mainly useful for development builds.
  • /MD - Dynamically link to the DLL of the non-debug multithreaded version of the CRT. This can reduce the size of your program's executable. This savings can be compounded if your application consists of multiple executables and DLLs, since they can share the same CRT code between them. This may require you to ship the CRT DLLs or for your users to install them separately though, which can be extra hassle and complexity.
  • /MDd - Dynamically link to the DLL of the debug multithreaded version of the CRT. Same as above, but added debugging info will run slower. Mainly useful for development builds.

Doing a quick test with a simple hello world program, the compiled executable ended up being about 12KB with /MD, while it weighed in at about 219KB with /MT, so the difference in size is considerable, especially if you're shipping lots of small programs.


There also used to be single-threaded versions of the library that could, in theory provide better single-thread performance and smaller size, but those are no longer provided with newer versions of the CRT. Presumably the difference was too minor for it to make sense shipping an entire separate build of the library.

Upvotes: 1

Paul Evans
Paul Evans

Reputation: 27577

Are those dependencies baked in to the program from the programmer's computer, or are they still left out and pulled from the user?

As @Igor comments, you can do either. The difference being a (perhaps rather large) single independent statically-linked executable file or one that depends on local DLLs to run.

Upvotes: 1

Related Questions