-->

CMake (Ninja back-end) compile with /MT

2019-07-14 19:50发布

问题:

I have a similar problem to this CMake compile with /MT instead of /MD but with some differences:

I'm using Visual Studio 2017 to build a library using CMake and Ninja generator. Visual Studio gives me some choices (configurations) like x86-Debug, etc. There is also a CMakeSettings.json file that seems I can manipulate Visual Studio's default configurations through it. But I don't know how!

I want to compile my library with /MT but I don't want to hard code it inside the CMakeLists.txt file like in the above post. That mean I want to compile my library based on the user choice through the configurations.

Is there any variable in CMakeSettings.json file that helps me to define /MT compiler switch? What about static/dynamic building of the library? Are there any help available about this file?

回答1:

I had the same problem a few weeks ago and I solved it using CMakeSettings.json file. I explain the solution for defining MT and the method for static/dynamic is the same.

Use cmakeCommandArgs variable inside CMakeSettings.json to send whatever you want as cmake arguments. It can be something like:

"cmakeCommandArgs": "-DLINK_TYPE=Static"

Then in CMakeLists.txt you must simply decide based on the argument you've defined.It can be in the following form:

if ("${LINK_TYPE}" STREQUAL "Static")
    add_compile_options(/MT$<$<CONFIG:Debug>:d>)
endif()