7

Is it possible to develop in C/C++ for windows and not to link against the msvcr100.dll?

I understand that this is the standard c library for windows, but I was wondering how all the programs on my computer could run if I hadn't Visual Studio or the Redistributable package installed?

KarSho
  • 5,699
  • 13
  • 45
  • 78
Erik
  • 11,944
  • 18
  • 87
  • 126
  • 1
    You can develop against other c++ libraries like Intel's or Gcc's, but if you dynamically link to the library and it's not available at runtime, your program won't run. Statically linking would work. – wkl May 17 '12 at 12:06
  • Against which library is Paint etc. linked? Or are they all statically linked with the c lib they use? – Erik May 17 '12 at 12:07
  • 1
    Paint is developed against an older msvcr and that one is already installed into windows by default. – wkl May 17 '12 at 12:09
  • 1
    You can check dynamic (DLL) dependencies of a program using `depends.exe` – Steve Townsend May 17 '12 at 13:20
  • @Steve. Thanks! Yes my executables always depend on msvcrXXX. – Erik May 17 '12 at 13:21

3 Answers3

16

Right-click your project in the Solution Explorer window, Properties, C/C++, Code Generation, Runtime Library setting. Change it to /MTd. Repeat for the Release configuration, pick /MT

You will now link the static version of the CRT, any functions you use get directly linked into your EXE instead of getting them from msvcr100.dll at runtime. So you no longer have the requirement to deploy the DLL along with your program.

Avoid using this option if you create your own DLLs. It then gets to be important that those DLLs and the EXE use the exact same CRT so they'll all share the same CRT state and the same heap. Because if they don't then you'll have nasty problems with passing C++ objects or pointers that need to be released from one chunk of code to another. An AccessViolation if you are lucky, a memory leak if you are not.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • The nasty errors are far less common nowadays, but it's still a waste of disk space, memory and CPU cache to have 20 DLLs with one copy of the CRT each. – MSalters May 18 '12 at 09:43
  • This doesn't always work for some reason. For example, if you've already compiled the project at least once, you can't link it like that. – Susan Yanders Aug 08 '13 at 21:04
  • That's vague, but don't expect incremental linking to work when you change it. Not a problem. – Hans Passant Aug 08 '13 at 21:08
2

If you restrict your project to use only C programming language/library, then you can only link against MSVCRT.lib which is completely baked in any Windows version since Windows XP SP3.

It means, that rather than dependency on MSVCR100.DLL (or any other Visual Studio DLL), you can only link against the standard C functions in MSVCRT. By the way, this technique is used in CoApp project developed under umbrella of Microsoft, so I'd consider it as a good pratice in such cases as yours.

Simply, download Windows DDK and link only against $(DDKInstallPath)lib\Crt\$(DDKPlatform)\msvcrt.lib

mloskot
  • 37,086
  • 11
  • 109
  • 136
  • 1
    Thanks. Thats indeed very interesting and more what I was looking for. – Erik May 17 '12 at 15:16
  • This library is an undocumented implementation detail. You can probably count on Microsoft to support it in the future, though, as many programs mistakenly depend on it. But that support will be limited, e.g. they're not going to port it to 64 bits since there never was a 64 bits VC6. – MSalters May 18 '12 at 09:51
  • @MSalters This is not true, completely. Install Windows DDK and check yourself. The DDK does provide amd64, i386 and ia64 builds of the msvcrt.lib. DDK is not dead! – mloskot May 19 '12 at 00:16
  • 1
    I also do this for my project - link against WDK 7.1 and against MSVCRT.DLL - github.com/malkia/ufo - Other folks doing it are the XChat-WDK - http://code.google.com/p/xchat-wdk/ – malkia Aug 21 '12 at 23:27
1

On Windows, I doubt it's possible to create a non-trivial program that doesn't use the CRT in some way.

It is possile to use the CRT without linking to msvcrXXX.dll -- simply link to the static libs instead. But to address your question:

how all the programs on my computer could run if I hadn't Visual Studio or the Redistributable package installed?

If the programs on your PC were linked to msvcrtxxx.dll, then they couldn't. Sinmply, the redist that a particular program needed was already installed on your PC before you even came along, probably. Or at least, the parts of the redist needed by the program.

John Dibling
  • 99,718
  • 31
  • 186
  • 324