8

I know that there are some questions about how to include msvcr120.dll/msvcp120.dll into your project.

But I want to drop that dependency. I compile the program in Release version, in Visual Studio 2013. I do not depend on any VS-specific commands (#pragma etc.) or precompiled headers etc.

I want to compile it to one single release .exe and provide it to user WITHOUT demanding him to install VC++ Redistributes for VS (the user will be working on Windows 7, Windows 8, maybe Windows XP).

Is that possible? If so, how?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
PolGraphic
  • 3,233
  • 11
  • 51
  • 108
  • 1
    You will have to use the static runtime if you do not want the redistributable. – drescherjm Dec 01 '14 at 19:50
  • Should I use /MT option then? And the same for any of .lib I compile and include into project? What with 3rd-party .dll files? – PolGraphic Dec 01 '14 at 19:54
  • 1
    `#pragma` and most VS specific commands don't depend on the msvcr. Having _globals_ depends on the c++ runtime. It's also the thing that calls main. You need that code. However, as drescherjm says, you can embed that lib code in your exe (via the `/MT` or `/MTd` option), and then you don't need a seperate dll file. – Mooing Duck Dec 01 '14 at 19:58
  • 1
    ***What with 3rd-party .dll files?*** You would have to avoid these. Although I thought you were already doing that with the single exe. – drescherjm Dec 01 '14 at 19:58
  • @drescherjm yes, I'm sorry - it was a simplification for the question. I want single .exe + opengl dll files (which are not part of VC and that's why I ignored them in the first place). Can you make an answer from your comment about MT? I can accept it as an answer. – PolGraphic Dec 01 '14 at 20:04

2 Answers2

12

You can statically link the runtime to your project by setting the /MT flag. You can find this option in Visual Studio 2013 under Project > [ProjectName] Properties... > Configuration Properties > C/C++ > Code Generation > Runtime Library. Make sure to only set it for the Release configuration.

Chris Vasselli
  • 13,064
  • 4
  • 46
  • 49
  • 1
    I would recommend this as an accepted answer because it clearly describes what it has to be done. Indicating the comments does not provide an eloquent answer for the end user. – George Netu May 04 '15 at 15:01
  • 1
    I was facing the same problem even after changing it to MT it still giving error message to msvcr120.dll not found – Digvijay Rathore Jul 15 '16 at 09:07
  • This doesn't work for me. Just like the guy above. I switched to /MT, my output file did not change size and I am still dependent on msvcr120.dll – Joseph May 02 '18 at 16:07
  • I got this to work but I also had to add the following additional command line arguments to the linker: /NODEFAULTLIB:msvcrt.lib /defaultlib:libcmt.lib – Joseph May 02 '18 at 16:30
3

From the comments. To remove the requirement of possibly needing the redistributable you can build your application with the static runtime (/MT option) instead of either of the dynamic runtime choices.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • Thanks for the answer. Are there any side-effects for this approach? It's possible to add the .lib that was not build with `/MT` option to project with `/MT` option? If not, how can I determine that e.g. some 3rd party library has been build with or without `/MT` option? – PolGraphic Dec 01 '14 at 20:32
  • 3rd party dlls will most likely use /MD which you want to prevent this is why you will need to avoid these. – drescherjm Dec 01 '14 at 20:38
  • @dreschermj - hm, ok. But still, it's very non-elegant to demand from user to instal VC++ redistributes in order to run my game/application. I personally don't like when installer tries to install them or other shared resources along with the app I wanted to install. – PolGraphic Dec 01 '14 at 20:40
  • 1
    For static libraries you will get a warning if the static library depends on a dynamic CRT. http://stackoverflow.com/questions/3007312/resolving-lnk4098-defaultlib-msvcrt-conflicts-with – drescherjm Dec 01 '14 at 20:43
  • In my applications (medical imaging research - so not used by many) I package the runtime in my installer and have it as an optional (default off) component in the installer. – drescherjm Dec 01 '14 at 20:45
  • ok, thanks once again :) I will use `/MT` option now. And in the future, if DLL built without `/MT` will be an only option, I can consider your approach used in medical imaging research. – PolGraphic Dec 01 '14 at 20:56