I am trying to understand the Microsoft Visual Studio C++ option "Runtime Library".
According to /MD, /MT, /LD (Use Run-Time Library) [MS Learn], there are 6 options. Those are essentially 3 options which come in Debug and Release flavor.
Where do I set the /LD option?
The website says:
- Open the project’s Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio.
- Select the Configuration Properties > C/C++ > Code Generation property page.
- Modify the Runtime Library property.
At that place, my Visual Studio only offers /MD, /MT, /MDd and /MTd. There is no /LD or /LDd option available.
From this statement at /LD:
Implies /MT unless you explicitly specify /MD.
I conclude that these have to be different options, otherwise I could not specify /LD and /MD at the same time.
I am using Visual Studio 2022 Preview, version 17.13.0 Preview 2.1. My project type is a "Dynamic-Link Library (DLL)". Configuration Properties / General / Configuration Type says "Dynamic library (.dll)".
These are the all the compiler command line arguments if I specify /MT for the debug configuration:
/JMC /permissive- /Yu"pch.h" /ifcOutput "x64Debug" /GS /W3 /Zc:wchar_t
/ZI /Gm- /Od /sdl /Fd"x64Debugvc143.pdb" /Zc:inline /fp:precise
/D "_DEBUG" /D "DYNAMICLIBRARY_EXPORTS" /D "_WINDOWS" /D "_USRDLL" /D "_WINDLL"
/D "_UNICODE" /D "UNICODE"
/errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MT /FC /Fa"x64Debug" /EHsc
/nologo /Fo"x64Debug" /Fp"x64DebugDynamicLibrary.pch" /diagnostics:column
Why? This example [MSDN] says I should compile using cl /EHsc /W4 /MD /LD test1Dll.cpp
. I know, I could either just do that on the command line, or I could add /LD
in the additional command line options of the UI. But I want to get a better understanding and not just blindy follow some instructions.
2
Answers
The current Microsoft command-line C/C++ compiler (CL.EXE) supports
/LD
just fine:If you only use Visual Studio, you might not be aware that the CL.EXE command-line compiler automatically calls the linker (LINK.EXE) after compiling so you can get the final product in one command. That is, unless you pass the
/c
(lowercase) argument to CL.EXE, "compile only, no link", which tells the compiler to act like a pure compiler and leave the linker out. And while having the compiler call the linker for you can be convenient for small programs, Visual Studio ALWAYS passes/c
because it wants maximum flexibility controlling the compiler and linker independently.The
/LD
(and/LDd
) options of the compiler exist to support people who use CL.EXE in its default compiler+link mode, by hand or from a batch file or a make file. The tell the compiler, to tell the linker, to make a DLL.You will not see an option to set a
/LD
flag in the Visual Studio Project Configuration dialog because the option doesn’t do anything if you pass the/c
argument (which is always the case in Visual Studio). When you select "Dynamic Library (.dll)" from the "Configuration Type" option, Visual Studio passes instead the/dll
argument directly to the linker when it calls it later down the road.Keep in mind that the online documentation you reference is for the CL.EXE command-line options. It’s written with a command-line user in mind, not Visual Studio. And while those options have a tight correlation to the options in a Visual Studio project, they are not an exact 100% match.
Finally, the reason
/LD
and/LDd
are grouped together in the documentation with/MT
,/MTd
, etc. is not because it has anything to do with the debug or release or single-vs-multithreaded version of the C-Runtime etc. libraries. Or "Use Runtime Library" for that matter like the title of the online documentation implies. They are grouped together because these flags are for the linker, not for the compiler. Yes, the title of the current documentation online is a bit misleading there, probably updated by someone focused on what most of the options do and losing sight on why the options were grouped together to begin with.In my tests, in fact,
/MT and /MD are configuration options for the runtime library settings.
/MD indicates that the runtime library is provided by the operating system as a DLL and is not embedded in the program. As a result, the compiled file is smaller. The library used is MSVCRT.lib.
/MT indicates that the runtime library is embedded within the program. This results in a larger compiled file. The library used is LIBCMT.lib.
/LD is used to control the output type as a DLL, and is unrelated to the runtime library.
/MT or /MD can be used together with /LD.
========================================================================
Since /LD is used to control the output type as a DLL and is unrelated to the runtime library, it is reasonable that this option is not available below.
I believe that the use of /LD should be more implicit, meaning that when you choose to create a DLL project, the compiler should automatically set /LD during compilation.