MT or MD for static release? - c++

In the static release of my application, I do not want the user to need the msvcrt runtime. My application depends on another library that I compile myself. Should this library use multithreaded or multithreaded DLL when compiling it? The library is static compiled.
Thanks

VC++'s license agreement prohibits the distribution of debug builds on any computer that doesn't already have VC++ installed, so your only option is to use /MTd or /MDd for debug builds while developing the application and /MT for the release build meant for distribution.

You should use DLL CRTs wherever possible, you can end up with trouble if you start linking multiple copies statically. If you know for a fact that you're compiling the final product, then you could link statically.

Related

Why should we set /MT to run executable in another pc

I'm reading about /MT and /MD, but I'm a little confused about it
HEAR is something I don't completely understand :
/MT Causes your application to use the multithread, static version of the run-time library. Defines _MT and causes the compiler to place the library name LIBCMT.lib into the .obj file so that the linker will use LIBCMT.lib to resolve external symbols.
what does it mean?
If you link with /MD or /MDd your program is going to need the CRT dlls in order to run. typically they are called something like msvcp100.dll for the C++ runtime and msvcr100.dll for the C runtime. If you are deploying your application using an installer, you can add a package with these to your installer so the dlls are going to be there when someone runs the application. If on the other hand you are going to deploy your application just as a single stand alone exe, your users are going to need a copy of these dlls. The latest versions of these dlls usually come with windows itself (not the debug ones) but if your user is running an older version of windows it may not have the needed dlls.
Linking your application to the static version of the CRT saves this whole headache for the price that the exe is slightly bigger (since it contains the CRT in it)
If you do use /MT (Static CRT) you have to make sure that everything else that you statically link with uses /MT as well. otherwise you'll end up with an executable where part of the code uses the static CRT and part is still depends on the CRT DLL. Other than defeating the basic purpose not needing the CRT DLL, this can also cause other problems.
To make sure what DLLs your exe depends on you can use the dependency walker.

Why do i need to specify runtime library type for static lib building?

Does it link against the runtime library when i build my static lib? Does it "put" the code from runtime lib into my lib?
Or is it just information for linker, so that when it links final exe(dll) it knows what version of runtime library to use for this particular static library?
Do i need to use the same version of runtime library in all my static libs and dlls?
Do i need to use the same type (/MT /MTd /MDd ...) in all my static libs and dlls?
And one more short question, is it usual that static windows libraries are twice the size of linux static libraries?
Or is it just information for linker, so that when it links final
exe(dll) it knows what version of runtime library to use for this
particular static library?
Yes.
Do i need to use the same version of runtime library in all my static
libs and dlls?
I strongly strongly advise it. You will get a mess of linker errors if you don't.
Do i need to use the same type (/MT /MTd /MDd ...) in all my static
libs and dlls?
Yes.
If you are releasing a DLL to be consumed by a 3rd-party, you may want to provide them with YourLibraryD.dll which uses the /MTd flag, and a YourLibrary.dll which uses /MT. Have different solution configurations for each. Nobody uses the single-threaded versions any longer, because the performance penalty is mostly irrelevant now and not worth the risk.
EDIT: Even if you aren't releasing to a 3rd party, you still want to make sure you link to the right DLL when in debug -vs- release mode. This is because when you build YourApp.exe in Debug, which uses /MTd, you will want it to link to YourLibraryD.dll. When you build YourApp.exe in Release, which uses /MT, you will want it to link to YourLibrary.dll in release mode. You could keep the DLL name the same and use the directory to disambiguate: so then you link to bin\debug\YourLibrary.dll in debug mode, and bin\release\YourLibrary.dll in release mode. Sorry if this is beyond your question, it is just good to know when you first switch build configurations and suddenly you start getting linker errors.

/MT version of binary crashes in Visual Studio 2008 - how to debug

I build /mt (run-time library:multi-threaded) and /md (run-time library:multi-threaded-dll) versions of my C++ DLL with visual studio 2008.
Applications which link to the /md build runs fine,
but applications which link to the /mt build crash.
Interestingly applications which link to the static version of the /mt build work fine.
Does it make sense to build a DLL with /mt and use it with an application which is also built with /mt?
How can I trace the reason for this sort of crash?
Regards,
Paul
It depends on your API. If you build your executables with the non-DLL version of the run time libraries then each DLL and EXE gets it's own copy of the static data of the run time libraries. The most obvious effect is that you can't allocate something dynamically from one module (DLL or EXE) and expect to safely free it in another module. There going to be other less common issues, for example if you srand in one module, don't expect it to affect calls to rand across the application.
It's generally safest in an executable that links against other user DLLs to compile them all against the DLL version of the run time libraries. You might want to use the static versions of the run time libraries if you were building a statically linked executable using static libraries, perhaps for ease of packaging and distribution but I don't see much benefit in a hybrid configuration given the potential issues.

Dll vs static lib (MSVC9 RunTime Library option)

For a MSVC9 Win32 project following options are shown under Configuration Properties -> C/C++ -> Code Geberation -> Runtime Library:
/MT, /MTd, /MD, /MDd
is it correct that for a DLL /MTd should be used and for static lib /MDd?
Thanks.
There are two issues that are at play here.
First, you need to choose if you want the Debug version of the CRT or the Release version. The debug versions have special checks and code paths designed to help you catch bugs while writing an application. You should not use them for the final release version of an application because they can slow down its execution, and because they are not freely redistributable.
Then, you need to decide if you want to statically link the run-time to your application, or if you want to use it dynamically from a DLL. Static linking allows you to create a standalone EXE file with no dependencies on any DLL files; it effectively compiles the run-time code into your application's binary. This can make deployment easier, but it comes at the cost of not being able to take advantage of security and other updates that are made to the run-time DLLs. You'll have to recompile your application in order to take advantage of the new run-time updates. Dynamic linking is the typical (and recommended) path for Windows applications. It means that your application will require the appropriate versions of the CRT DLLs to be present in order for it to run, but it allows the run-time libraries to be easily updated and means that multiple programs can share the same code, reducing their size on disk.
So, /MD means dynamically-linked and /MT means statically-linked. The lower-case d after each option indicates that the debug version of the run-time libraries is used.
/MD = dynamically-linked to release (redistributable) version of CRT
/MDd = dynamically-linked to debug (non-redistributable) version of CRT
/MT = statically-linked to release (redistributable) version of CRT
/MTd = statically-linked to debug (non-redistributable) version of CRT
More information is available on MSDN.

Removing msvcp90d.dll dependancy from Windows binary

I have a dll that I distribute that will not run on some windows OS. Using dependancy walker I discover msvcp90d.dll missing on these systems. I DO NOT want any run time dependancies that require the C++ redistributable, and since the application that calls the DLL is not written in C++, it does not have any dependancy on the C++ redistributable.
I am guessing the I left the DEBUG option in the linker preferences on when I compiled the dll which is why it needs msvcp90d.dll?
ADDED:
Appologies, I pasted the wrong dll name in my original question.... too many hours in front of the monitor...
THe dll is a third party dll that I did not write compiled by me in VS2008.
MSVCP90 is nothing to do with debug (that'd be msvcp90d). You can remove your dependency by switching the compiler to /MT (instead of /MD). You also need to ensure that every static library you link to was also compiled /MT.
I recommend against building apps /MT because it has a significant negative effect on system performance and makes servicing take longer in the event of a security issue with the CRT.
Finally, note that /MT means that your CRT is private. So you must ensure that CRT/STL types don't pass across your DLL boundary.
Martyn
Your options as I see them:
Compile the DLL with the /MT option to use static linking to the C runtime.
Continue with dynamic linking to the runtime, but distribute the C runtime with your app.
It needs MSVCP90.dll because the dll was compiled with Visual Studio 2008 most likely. That is the release runtime. The short answer is if you don't want C++ runtime dependencies don't use C++ libraries or applications.
However you can do any of the following to solve your problem:
Install the redistributable to the target system to satisfy the dependency
Remove the dependency on that dll from your application
Recompile the dll against the version of VC you prefer that is already present on the target system