配置操作如图,源码见底部附录部分
安装CLion
官网下载
创建项目
设置环境
调整类型为release
配置opencv和项目
编译环境
编译后
重启CLion
测试opencv环境
测试代码
运行main.cpp显示图片
测试torch环境
没标红表示配置成功
附件
CMakeList.txt
cmake_minimum_required(VERSION 3.28)
project(test_20240419_C++)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_PREFIX_PATH E:/lib/libtorch-win-shared-with-deps-2.2.2+cpu/libtorch)
set(OpenCV_DIR E:/lib/opencv/build/x64/vc16/lib)
find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(test_20240419_C++ main.cpp)
target_link_libraries(test_20240419_C++ ${TORCH_LIBRARIES} ${OpenCV_LIBS})
set_property(TARGET test_20240419_C++ PROPERTY CXX_STANDARD 17)
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET test_20240419_C++
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:test_20240419_C++>)
file(GLOB OPENCV_DLLS "E:/lib/opencv/build/x64/vc16/bin/*.dll")
add_custom_command(TARGET test_20240419_C++
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${OPENCV_DLLS}
$<TARGET_FILE_DIR:test_20240419_C++>)
endif (MSVC)