c++cmakevcxproj

How to customize vcxproj generation by CMake


I want to use CMake to generate .vcxproj files. Then I open specific project/solution and work in VS as usual. How do I customize default configurations, generated by VS generator? In particular, how do I specify in my cmake files (whatever it should be) that I need release runtime for "Debug" configuration? (i.e. /MD instead of /MDd).

As far as I understand, such an option is VS-specific (VS/MSBuild flag) and thus is not manupulated my CMake naturally.


Solution

  • In particular, how do I specify in my cmake files (whatever it should be) that I need release runtime for "Debug" configuration? (i.e. /MD instead of /MDd).

    This will work:

    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL"
        CACHE STRING "MSVC runtime library selection")
    

    Then all of the targets you build in all configurations will use -MD. See the documentation here: https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html

    The default value is MultiThreaded$<$<CONFIG:Debug>:Debug>DLL which uses -MDd for the Debug configuration and -MD for all others. This uses generator expressions, which are out of scope for this question.


    An alternative: you can lean in to CMake's defaults and use the RelWithDebInfo build config. If you need to override the optimization flags, you can always set CMAKE_CXX_FLAGS_RELWITHDEBINFO.