使用clion写c++,使用cmake编译 opencv和libtorch,使用的是window11系统。
-
- 其中项目名称叫
torch_use0124
- 其中项目名称叫
-
- libtorch文件夹路径为:
C:/Users/yuanz/Downloads/cpp_library/libtorch
- libtorch文件夹路径为:
-
- opencv文件夹路径为:
C:/Users/yuanz/Downloads/cpp_library/opencv
- opencv文件夹路径为:
cmake_minimum_required(VERSION 3.21)
project(torch_use0124)
set(CMAKE_CXX_STANDARD 17)
add_executable(torch_use0124 main.cpp Darknet.cpp Darknet.h)
# opencv
set(OpenCV_DIR C:/Users/yuanz/Downloads/cpp_library/opencv/build/x64/vc15/lib)
include_directories(C:/Users/yuanz/Downloads/cpp_library/opencv/sources/include)
find_package(OpenCV REQUIRED )
target_link_libraries(torch_use0124 ${OpenCV_LIBS})
# libtorch
set(CMAKE_PREFIX_PATH C:/Users/yuanz/Downloads/cpp_library/libtorch)
include_directories(C:/Users/yuanz/Downloads/cpp_library/libtorch/include)
include_directories(C:/Users/yuanz/Downloads/cpp_library/libtorch/include/torch/csrc/api/include)
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
target_link_libraries(torch_use0124 "${TORCH_LIBRARIES}")
set_property(TARGET torch_use0124 PROPERTY CXX_STANDARD 17)
# The following code block is suggested to be used on Windows.
# According to https://github.com/pytorch/pytorch/issues/25457,
# the DLLs need to be copied to avoid memory errors.
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET torch_use0124
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:torch_use0124>)
endif (MSVC)