Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio: How to specify different runtime libraries for the linker? (/MTd, MDd, etc)

I'm linking to a few libraries in VS2008. If my knowledge of the linker is correct, MTd is for static linking and MDd is for dynamic linking (to a DLL.) My goal is to statically link some libraries and dynamic link others. The project options seems to only have one setting for all libraries in the linker input. How would I do this?

like image 269
Thomas Havlik Avatar asked Jan 21 '23 00:01

Thomas Havlik


2 Answers

Your project will be given a sensible C Runtime Library default after you set it up, depending on how you answer the New Project Wizard prompts. You can inspect and alter this (if needed) as follows:

  • right-click the relevant project in Solution Explorer, select Properties
  • look under Configuration Properties, C/C++, Code Generation, Runtime Library.

Other libraries can be linked however you want, you just specify the library to link to under Linker, Input, Additional Dependencies.

Even if you are linking to a DLL, it will still have a .LIB file (of the correct form for a DLL) to resolve external references, unless you are manually loading the DLL and discovering required function entry points.

You do need to make sure that the LIB files you link to use the same CRT as your app does, or things can go unexpectedly wrong.

like image 198
Steve Townsend Avatar answered Feb 05 '23 17:02

Steve Townsend


No, you're mixing it up. The /MD vs /MT options is only relevant to which CRT version you link. There are two, the static version (/MT) which you should use only if you don't use any DLLs in your project. And the DLL version, a version that every binary in your process can share so that you won't have heap allocation misery. The kind of misery you get into when memory is allocated by one module and freed by another.

Choosing your own libraries is entirely up to you. Mixing and matching is fine, the linker just gets another kind of .lib. An import library instead of a static library. Just keep in mind to use /MD when you use DLLs.

like image 23
Hans Passant Avatar answered Feb 05 '23 16:02

Hans Passant