Windows下 CLion中,配置 OpenCV、LibTorch

首先按照win下C++部署深度学习模型之clion配置pytorch+opencv教程记录 步骤配置。

LibTorch 部分

在测试LibTorch时会出现类似 c10.dll not found 的问题(Debug才有):

参考C++部署Pytorch(Libtorch)出现问题、错误汇总
如何在windows10上使用cmake配置libtorch(vs2022版)

主要是MSVC 需要这些 dll文件 。 可以如下修改CmakeList.txt让它找到:

clike 复制代码
cmake_minimum_required(VERSION 3.12)
project(opencv_test)

set(CMAKE_CXX_STANDARD 17)

#set(CMAKE_PREFIX_PATH C:/Libraries/libtorch-win-shared-with-deps-2.3.0+cu118/libtorch)
list(APPEND CMAKE_PREFIX_PATH C:/Libraries/libtorch-win-shared-with-deps-2.3.0+cu118/libtorch)		# 这里指定的话不必将Torch的路径加入环境变量
#set(Torch_DIR "E:/libtorch/share/cmake/Torch")
#include_directories("E:/libtorch/include")
#include_directories("E:/libtorch/include/torch/csrc/api/include")
find_package(Torch REQUIRED)
message(STATUS "torch status: ${TORCH_FOUND}")
message(STATUS "torch library: ${TORCH_LIBRARIES}")
message(STATUS "TORCH_INSTALL_PREFIX: ${TORCH_INSTALL_PREFIX}")


add_executable(opencv_test main.cpp)
target_link_libraries(opencv_test ${TORCH_LIBRARIES})

if (MSVC)
    file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
    add_custom_command(TARGET opencv_test
            POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            ${TORCH_DLLS}
            $<TARGET_FILE_DIR:opencv_test>)
endif (MSVC)

if (CMAKE_VERSION VERSION_GREATER 3.12)
    set_property(TARGET opencv_test PROPERTY CXX_STANDARD 17)
endif()

OpenCV 部分

clion编译OpenCV时,install的位置在这(更多参考):

不能用clion编译,还是会出现找不到 dll文件的问题!!!

参考如下链接用VS编译:

Windows 下使用 CMake + Visual Studio 2022 编译 OpenCV 4.8.1 及其扩展模块 (主要参考)

vs2019编译opencv

注: Opencv_world勾选后后面build可能会出问题,不知道怎么解决建议别勾。

之后记得把 D:\prj\opencv\opencv4\out\install\x64-Debug\x64\vc17\bin添加到环境变量。

之后在camke时你可能又出现了问题

bash 复制代码
CMake Warning at C:/Libraries/opencv-4.8.1/cmake-build-release/install/OpenCVConfig.cmake:190 (message):
  Found OpenCV Windows Pack but it has no binaries compatible with your
  configuration.

  You should manually point CMake variable OpenCV_DIR to your build of OpenCV
  library.
Call Stack (most recent call first):
  CMakeLists.txt:8 (find_package)


CMake Error at CMakeLists.txt:8 (find_package):
  Found package configuration file:

    C:/Libraries/opencv-4.8.1/cmake-build-release/install/OpenCVConfig.cmake

  but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be
  NOT FOUND.

这是因为OpenCV_DIR 应当精确到lib!!! 故意的还是不小心的???

clike 复制代码
set(OpenCV_DIR C:/Libraries/opencv-4.8.1/cmake-build-release/install/lib)

完整工程文件

CmakeList.txt

clike 复制代码
cmake_minimum_required(VERSION 3.12)
project(opencv_test)
set(CMAKE_CXX_STANDARD 17)

set(CMAKE_BUILD_TYPE "Release")


set(OpenCV_DIR C:/Libraries/opencv-4.8.1/build/install/lib)  # 这里要精确到lib!
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
message(STATUS "OpenCV status:" ${OpenCV_FOUND})
message(STATUS "OpenCV Libs:" ${OpenCV_LIBS})


set(CMAKE_PREFIX_PATH C:/Libraries/libtorch-win-shared-with-deps-2.3.0+cu118/libtorch)
#set(Torch_DIR "E:/libtorch/share/cmake/Torch")
#include_directories("E:/libtorch/include")
#include_directories("E:/libtorch/include/torch/csrc/api/include")
find_package(Torch REQUIRED)
message(STATUS "torch status: ${TORCH_FOUND}")
message(STATUS "torch library: ${TORCH_LIBRARIES}")
message(STATUS "TORCH_INSTALL_PREFIX: ${TORCH_INSTALL_PREFIX}")



add_executable(opencv_test main.cpp)
target_link_libraries(opencv_test
        ${TORCH_LIBRARIES}
        ${OpenCV_LIBS}
)

if (MSVC)
    file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
    file(GLOB OpenCV_DLLS "${OpenCV_DIR}/../bin/*.dll")
    add_custom_command(TARGET opencv_test
            POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            ${TORCH_DLLS} ${OpenCV_DLLS}
            $<TARGET_FILE_DIR:opencv_test>)
endif (MSVC)

main.cpp

cpp 复制代码
#include <iostream>
#include <torch/script.h>
#include <torch/torch.h>

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>



void test_libtorch_version() {
    if (torch::cuda::cudnn_is_available())
    {
        std::cout << "cuDNN is available." << std::endl;
    }
    else
    {
        std::cout << "cuDNN is not available." << std::endl;
    }
    if (torch::cuda::is_available())
    {
        std::cout << "CUDA is available." << std::endl;
    }
    else
    {
        std::cout << "CUDA is not available." << std::endl;
    }
    std::cout << "Device count : " << torch::cuda::device_count() << std::endl;

}

int main(int, char**)  {
    torch::Tensor a = torch::rand({2, 3});
    std::cout << a << std::endl;


    test_libtorch_version();
    torch::Tensor b = torch::randint(10, 20, { 3, 2 });
    std::cout << b << std::endl;


    std::string path = "C:\\Users\\THM\\Documents\\Projects\\test_openvins_py\\data\\coco_bike.jpg";
    cv::Mat im = cv::imread(path);
    cv::imshow("image", im);
    cv::waitKey(0);
    return 0;
}

还有问题! -- 不能debug???

是的,上面只是编译了OpenCV的Release版本,想要debug你还得编译一遍debug版的!

怎么编呢?

你应该记得我们在编译时,将VS的编译模式改成了release模式,现在只需要调回debug模式,

然后再分别Build ALL_BUILD和INSTALL即可。

期间会遇到的编译问题是:

Debug build error: LNK1104: cannot open file 'python311_d.lib'

不会有人还装了debug版的python吧?!

如果你也没有,可以参照论坛, 找到你python路径下的PATH_TO_PYTHON_DIST/include/pyconfig.h, 修改:

cpp 复制代码
pragma comment(lib,"pythonxx_d.lib")

to

pragma comment(lib,"pythonxx.lib")

and

#       define Py_DEBUG

to

//#       define Py_DEBUG

还是不行???

另外创建一个转为debug而设置的文件夹build_debug,从Cmake开始再重走一遍流程。

最后可将build_debug里的东西复制到之前的build文件夹内(重复文件直接跳过)。

当然不嫌麻烦的话不复制也行,可以在CMakeList.txt里根据CMAKE_BUILD_TYPE指定对应build内的lib。

Last

最后附一个:Build OpenCV (including Python) with CUDA on Windows

相关推荐
3D打印资源库2 分钟前
官宣:汇纳科技收购华速实业;融速科技完成A+轮融资;3D打印单季破40亿美元|库周报
人工智能·科技·3d
独自归家的兔3 分钟前
大模型通义千问3-VL-Plus - QVQ 视觉推理模型
java·人工智能·intellij-idea
中华网商业6 分钟前
从制造到智造!格力金湾领航级智能工厂的升级路径与经验启示
人工智能·制造
数据的世界018 分钟前
重构智慧书-第12条:自然与人工的辩证之美
人工智能
爱写代码的小朋友8 分钟前
AI赋能的混合式教育模式中师生角色重构与互动机制研究
人工智能
AI即插即用10 分钟前
即插即用系列 | MICCAI EM-Net:融合 Mamba 与频域学习的高效 3D 医学图像分割网络
网络·人工智能·深度学习·神经网络·学习·计算机视觉·视觉检测
阿杰学AI13 分钟前
AI核心知识53——大语言模型之Structured CoT 超级模版(简洁且通俗易懂版)
人工智能·ai·语言模型·prompt·提示词·pe·structured cot
hellocode_13 分钟前
【2025年】GPT-5.2怎么样?Instant/Thinking/Pro 怎么选?如何订阅使用chatgptplus?GPT-5.2使用实例展示
人工智能·gpt·chatgpt
咚咚王者21 分钟前
人工智能之数学基础 线性代数:第二章 向量空间
人工智能·线性代数
skywalk816324 分钟前
SCNet 双DCU异构卡vllm推理部署DeepSeek-Coder-V2-Lite-Instruct
人工智能·vllm·scnet·deepseek-coder