• CLion配置libtorch找不到xxx.dll


    项目场景:

    使用CLion配置libtorch时遇到该问题


    问题描述

    使用CLion配置libtorch时,CMakeLists.txt文件写完后,cmake也能成功,但是一旦运行代码就会报错找不到xxx.dll,比如找不到torch_cuda.dll或找不到c10.dll


    原因分析:

    其实并不是因为你下载的libtorch中没有xxx.dll这些文件,而是因为CLion找不到,你得手动告诉他位置在哪里。这些dll文件都在libtorch/lib下。


    解决方案:

    在使用CLion运行CMake Application时手动加上环境变量,如下图所示:
    在这里插入图片描述
    在这里插入图片描述
    Environment variables这栏加上libtorch/lib的路径
    这时再运行下面代码即可正确加载

    下面贴上我的完整的CMakeLists.txt内容:

    cmake_minimum_required(VERSION 3.25)
    project(torch_cuda_cpp)
    
    set(CMAKE_CXX_STANDARD 17)
    set(Torch_DIR E:/CLion/libtorch/share/cmake/Torch)
    find_package(Torch REQUIRED)
    
    include_directories(E:/CLion/libtorch/include)
    include_directories(E:/CLion/libtorch/include/torch/csrc/api/include)
    
    add_executable(torch_cuda_cpp main.cpp)
    target_link_libraries(torch_cuda_cpp ${TORCH_LIBRARIES})
    set_property(TARGET torch_cuda_cpp PROPERTY CXX_STANDARD 17)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    特别提醒

    libtorch分为cuda版和cpu版,其实这和pytorch的cuda版和cpu版是差不多一个一次,只是libtorch是针对C++语言编写的。libtorch的cuda和cpu版的配置方法差不多,网上有很多人介绍怎么配置cpu版的,cuda版的方法也一样,只是别的博客大多只讲了怎么配置,而没有讲遇到的问题怎么解决,就比如我这篇博客里的问题。我踩过的坑记录一下,下面贴一段测试代码:

    #include 
    #include 
    
    int main() {
        torch::Tensor tensor = torch::rand({2, 3});
        std::cout << tensor << std::endl;
        std::cout << torch::cuda::is_available() << std::endl;
        std::cout << "Hello, World!" << std::endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

  • 相关阅读:
    css transition属性
    医药行业投资公司都有哪些?医药企业项目投资分析实用工具
    replace、replaceAll、replaceFirst的区别
    python爬虫之爬取携程景点评价(5)
    React-Router(V6版本)
    电阻值的优先值
    【蓝桥杯Web】第十三届蓝桥杯(Web 应用开发)第一次线上模拟赛
    HNSW-分层可导航小世界 算法学习
    52 html流星雨 图片 游戏
    中远麒麟堡垒机SQL注入漏洞复现
  • 原文地址:https://blog.csdn.net/qq_42940160/article/details/134327557