Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake: correct way to find/add visual studio or windows sdk libraries and headers?

I'm using cmake to generate a Visual Studio project.

Let's say I need to use the following Microsoft-specific libraries and headers:

  • Microsoft C++ Unit Testing Framework (example library location: c:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\VS\UnitTest\lib\x64\Microsoft.VisualStudio.TestTools.CppUnitTestFramework.lib)
  • crtdbg for finding memory leaks (example header location: c:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt\crtdbg.h)

I don't want to hardcode these paths into my CMakeLists.txt, or force a developer to pass in the paths during the build.

I expect to be able to do something like this:

# 1.  get include paths somehow
# 2.  get lib paths somehow

include_directories(
    ${MSVS_UnitTest_INCLUDE_DIR}
    ${Windows_SDK_INCLUDE_DIR}/ucrt
)

link_directories(
    ${MSVS_UnitTest_LIBRARIES_DIR}/x64
    ${Windows_SDK_LIBRARIES_DIR}/ucrt/x64
)

add_library(MyTest mytest.cpp)

target_link_libraries(MyTest
    libucrt
    Microsoft.VisualStudio.TestTools.CppUnitTestFramework
)

What's the right way to do #1 and #2?

like image 767
Kevin Avatar asked Sep 09 '19 23:09

Kevin


People also ask

How do I add a header to CMake?

To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .


1 Answers

At least for #1 (incldue paths to sdk) i found a solution. If you use the Generator for Visual Studio, the cmake variable CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION is filled with the paths to the include folder. Another way is using the environment variables $ENV{WindowsSdkDir} and $ENV{WindowsSDKVersion}, which is most likely the source cmake gets this information from as well.

like image 103
DerKasper Avatar answered Sep 21 '22 16:09

DerKasper