'How to conditionally link against a library using Visual Studio

Is there a mechanism (command line arguments, environment variables, config settings, or anything else) that would allow, within the same Visual Studio project, to conditionally link against a library based on the target Windows version?

Specifically, I want to only link in a particular library if the target Windows version is newer than Windows Vista.

I compile on Windows 10, and I want to produce two builds:

  • One build for Windows Vista that does not link in the library (since the library is not compatible with Vista)
  • One build for Windows 7 and newer that does link in the library

I am compiling using Visual Studio 2015.



Solution 1:[1]

The easy way would be to create multiple solution configurations (one for 7 and newer, the other for vista) then add the library to the newer configuration. This would be done first under Build ->Configuration Manager, and in the Configuration combo select New, You will want to create Debug/Release pair for "Debug - Vista", "Release - Vista" and then use "Edit" to change the existing "Debug" and "Release" to "Debug - 7" and "Release - 7".

After doing this, go into the project settings and change to your new configurations and pass however you specify windows version as preprocessor directives and add the library.

That way you can perform either build without changing any actual code or even having to change the preprocessor directives passed on the command line on an ongoing basis.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SoronelHaetir