Search code examples
c++opencvcmakevcpkg

Setting property MSVC_RUNTIME_LIBRARY doesn't change the runtime library


I have the exact same problem as stated in this question, but with a different configuration:

I am using OpenCV via VCPKG manifest mode and the cmake is as follows:


cmake_minimum_required(VERSION 3.10)

File(TO_CMAKE_PATH $ENV{VCPKG_ROOT} VCPKG_ROOT)
message(STATUS ${VCPKG_ROOT})
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
set(VCPKG_TARGET_TRIPLET "x64-windows-static-ffmpeg" CACHE STRING "")

project(test_project)


find_package(OpenCV REQUIRED)

set(INCLUDE_DIR "src/include")  # Change this if your library directory has a different path
include_directories(${INCLUDE_DIR})

# Set C++ standard to 20
set(CMAKE_CXX_STANDARD 20)

file(GLOB SRC_FILES "src/*.cpp" "src/*.c" )  # You can change "*.cpp" to "*.c" if your project uses C
file(GLOB INCLUDE_FILES  "${INCLUDE_DIR}/*.h" "${INCLUDE_DIR}/*.hpp") 

# Add an executable (replace "main.cpp" with your actual source file)
add_executable(test_program ${SRC_FILES} ${INCLUDE_FILES})
set_target_properties(test_program PROPERTIES
        MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
        )
# Include directories for header files
target_link_libraries(test_program ${OpenCV_LIBS})

and vcpkg is:

{
    "version": "1.0.0",
    "dependencies": [
        "opencv[core]"
    ]
}

and it is generating this error:

mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj  test_program opencv_core4d.lib(matrix.cpp.obj)      

I also tested this version of cmake and it did not worked:

cmake_minimum_required(VERSION 3.10)

File(TO_CMAKE_PATH $ENV{VCPKG_ROOT} VCPKG_ROOT)
message(STATUS ${VCPKG_ROOT})
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
set(VCPKG_TARGET_TRIPLET "x64-windows-static-ffmpeg" CACHE STRING "")

project(test_project)


find_package(OpenCV REQUIRED)

set(INCLUDE_DIR "src/include")  # Change this if your library directory has a different path
include_directories(${INCLUDE_DIR})

# Set C++ standard to 20
set(CMAKE_CXX_STANDARD 20)

file(GLOB SRC_FILES "src/*.cpp" "src/*.c" )  # You can change "*.cpp" to "*.c" if your project uses C
file(GLOB INCLUDE_FILES  "${INCLUDE_DIR}/*.h" "${INCLUDE_DIR}/*.hpp") 

# Add an executable (replace "main.cpp" with your actual source file)
add_executable(test_program ${SRC_FILES} ${INCLUDE_FILES})
set_property(TARGET test_program PROPERTY
             MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
# Include directories for header files
target_link_libraries(test_program ${OpenCV_LIBS})

I delete the build directory on each run of make to ensure that nothing is cached and project generation is always started from scratch.

How can I fix this?


Solution

  • Property MSVC_RUNTIME_LIBRARY appears only in CMake 3.15. In previous CMake versions it is ignored.

    You need to update your cmake_minimum_required command:

    cmake_minimum_required(VERSION 3.15)
    

    More precisely, with your code the property is ignored because of policy CMP0091, which is set to OLD when required CMake version is 3.14 or lower.

    But normally, if some feature is introduced in CMake version V, then for use this feature a code should specify minimal CMake version equal to V or newer.