我遇到了与这个问题中所述完全相同的问题,但配置不同:
我通过 VCPKG 宣言模式使用 OpenCV,cmake 如下:
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})
vcpkg 是:
{
"version": "1.0.0",
"dependencies": [
"opencv[core]"
]
}
并且它正在生成此错误:
mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj test_program opencv_core4d.lib(matrix.cpp.obj)
我也测试了这个版本的cmake,但没有成功:
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})
我在每次运行 make 时删除构建目录,以确保没有缓存任何内容,并且项目生成始终从头开始。
我该如何解决这个问题?
属性 MSVC_RUNTIME_LIBRARY 仅出现在 CMake 3.15 中。在以前的 CMake 版本中它被忽略。
您需要更新您的
cmake_minimum_required
命令:
cmake_minimum_required(VERSION 3.15)
更准确地说,在您的代码中,由于政策CMP0091,该属性被忽略,当所需的 CMake 版本为 3.14 或更低时,该属性设置为 OLD。
但通常情况下,如果 CMake 版本
V
中引入了某些功能,那么要使用此功能,代码应指定等于 V
或更高版本的最小 CMake 版本。