【问题标题】:Torch CMake Error at CMakeLists.txt:10 (find_package) in C++C++ 中 CMakeLists.txt:10 (find_package) 的 Torch CMake 错误
【发布时间】:2019-05-18 01:28:50
【问题描述】:

我正在尝试通过调用 torch/torch.hCMake 创建一个 Eclipse C++ 项目。我运行cmake -G "Eclipse CDT4 - Unix Makefiles" ./ 来创建一个Eclipse 项目,但我得到了这个错误:

-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Could not determine Eclipse version, assuming at least 3.6 (Helios). Adjust CMAKE_ECLIPSE_VERSION if this is wrong.
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:10 (find_package):
  By not providing "FindTorch.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Torch", but
  CMake did not find one.

  Could not find a package configuration file provided by "Torch" with any of
  the following names:

    TorchConfig.cmake
    torch-config.cmake

  Add the installation prefix of "Torch" to CMAKE_PREFIX_PATH or set
  "Torch_DIR" to a directory containing one of the above files.  If "Torch"
  provides a separate development package or SDK, be sure it has been
  installed.


-- Configuring incomplete, errors occurred!

其中CMakeLists.txt所在的当前目录有:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(test)        
find_package(Torch REQUIRED)    
add_executable(test test.cpp)
target_link_libraries(test "${TORCH_LIBRARIES}")
set_property(TARGET test PROPERTY CXX_STANDARD 11)

显然,它找不到TorchConfig.cmaketorch-config.cmake 文件;不过,我在/home/afshin/libtorch/share/cmake/Torch 中有 TorchConfig.cmake。我通过测试每个将相应的路径添加到CMakeLists.txt文件中:

set(CMAKE_MODULE_PATH "/home/afshin/libtorch/share/cmake/Torch;${CMAKE_MODULE_PATH}")
set(CMAKE_MODULE_PATH "/home/afshin/libtorch/share/cmake;${CMAKE_MODULE_PATH}")
set(CMAKE_MODULE_PATH "/home/afshin/libtorch;${CMAKE_MODULE_PATH}")
set(Torch_DIR "/home/afshin/libtorch;${Torch_DIR}")
set(Torch_DIR "/home/afshin/libtorch/share/cmake/Torch;${Torch_DIR}")
set(Torch_DIR "/home/afshin/libtorch/share/cmake;${Torch_DIR}")
set(DCMAKE_PREFIX_PATH "/home/afshin/libtorch/share/cmake/Torch;${DCMAKE_PREFIX_PATH}")
set(DCMAKE_PREFIX_PATH "/home/afshin/libtorch/share/cmake;${DCMAKE_PREFIX_PATH}")
set(DCMAKE_PREFIX_PATH "/home/afshin/libtorch;${DCMAKE_PREFIX_PATH}")

但是,它没有帮助,我仍然得到同样的错误。 我感谢任何帮助或 cmets。

我也尝试了 cmake-gui,我得到了同样的错误:

谢谢, 阿夫辛

【问题讨论】:

  • 那么这个文件在哪里?
  • 你能运行 cmake-gui 并检查平面路径是否被拾取?
  • @MatthieuBrucher 它在我的 Eclipse 工作区的一个新目录下: ls eclipse-workspace/test_cmake/ CMakeCache.txt CMakeFiles CMakeLists.txt src
  • CMake 正在寻找的 .cmake 文件。
  • 嗯,您的问题帖子恰恰相反:“显然,它找不到 TorchConfig.cmaketorch-config.cmake 文件。”。您能否更新问题帖子以反映实际情况?如果你有那个文件,在缓存中设置Torch_DIR 应该可以工作。我不知道为什么没有(根据屏幕截图,您已经尝试过但失败了)。请注意,Torch_DIR 本质上是 single-value 变量,将其设置为列表将不起作用。您也可以尝试将变量CMAKE_PREFIX_PATH 设置为/home/afshin/libtorch

标签: c++ cmake torch


【解决方案1】:

我能够通过将CMakeLists.txt 编辑为:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(test_cmake)

set(CMAKE_PREFIX_PATH "/home/afshin/libtorch/share/cmake/Torch")
find_package(Torch REQUIRED)

add_executable(test_cmake ./src/test_cmake.cpp)
target_link_libraries(test_cmake "${TORCH_LIBRARIES}")
set_property(TARGET test_cmake PROPERTY CXX_STANDARD 11)

或者,使用cmake-gui 也可以通过以下设置解决问题:

点击configure,然后点击generate。 最后,我通过选择Makefile Project With Existing Code 将这个项目导入Eclipse。此代码已成功编译和构建。

【讨论】:

    【解决方案2】:

    以下修改后的CMakeLists.txt 文件在没有明显缺失的TorchConfig.cmake (在vcpkg 安装here 中也缺失)的情况下工作。我推荐微软的vcpkg 用于跨平台包(c++ 库)管理(用法here)。但是代码是 vcpkg 独立的:可以将TORCH_BASE_PATH(或Torch_INCLUDE_DIRTorch_LIBRARIES)设置为正确的路径。

    #[[
        tested with:
        - CMake 3.13
        - Visual Studio Community Edition 15.9.4
            (CMake generator: "Visual Studio 15 2017 Win64")
        - torch-th library installed with vcpkg
            generic: vcpkg install torch-th
            for macOS: vcpkg install torch-th:x64-osx-dynamic
                x64-osx-dynamic triplet must be created: x64-osx + "set(VCPKG_LIBRARY_LINKAGE dynamic)"
            for Windows: vcpkg install torch-th:x64-windows
    
        - easy torch sample: https://apaszke.github.io/torch-internals.html
        ]]
    cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
    project(test)
    
    # cannot work without a "package configuration file" (TorchConfig.cmake)
    #   so we replace it with find_path and find_library
    #find_package(Torch REQUIRED)
    
    #[[ 
        the environement variable VCPKG_ROOT used here, contains the path to vcpkg installation folder
            replace the two paths with your paths to TH installation
    
        usage: #include "TH/TH.h"
        ]]
    set(TORCH_BASE_PATH "$ENV{VCPKG_ROOT}/installed/${VCPKG_TARGET_TRIPLET}")
    message(STATUS TORCH_BASE_PATH=${TORCH_BASE_PATH})
    
    set(Torch_INCLUDE_DIR "${TORCH_BASE_PATH}/include")
    set(Torch_LIBRARIES "${TORCH_BASE_PATH}/lib")
    # target_link_libraries is to be preferred
    #link_directories(${Torch_LIBRARIES})
    find_library(LIBRARY_TORCH TH HINTS ${Torch_LIBRARIES})
    #[[ 
        even simpler
        if you use the vcpkg toolchain file "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
    
            find_path(Torch_INCLUDE_DIR TH/TH.h)
            find_library(LIBRARY_TORCH TH)
        ]]
    
    add_executable(test test.cpp)
    target_include_directories(test PRIVATE ${Torch_INCLUDE_DIR})
    target_link_libraries(test ${LIBRARY_TORCH})
    
    set(CMAKE_CXX_STANDARD 11)
    #set_property(TARGET test PROPERTY CXX_STANDARD 11)
    

    【讨论】:

    • 感谢您的建议。正如我在 cmets 中提到的那样,我通过将 /home/afshin/libtorch/share/cmake/Torch 添加到 CMAKE_PREFIX_PATH 来解决它。另外,我认为您建议的代码适用于 Windows,我是 Linux 用户。
    • 一点也不,它是跨平台的。我将在一分钟内测试它,我在 Windows 中,但它是 BootCamp,所以我将切换到 macOS。另一方面,我不使用CMAKE_PREFIX_PATH,我更喜欢find_pathfind_library
    • 是的,我看到它正在尝试自动解决问题。作为 Windows,因为我在根中看到 x64-windows,所以我说提供的代码适用于 Windows。我了解到vcpkg 是一个跨操作系统工具。
    • 它在 macOS 下构建,但更棘手:需要一个新的三元组(而不是默认的 x64-osx)。
    • 三个问题:在windows下,如何安装Vcpkg?我看到有一个 Visual Studio 要求。我可以改用 Eclipse 吗?您发送的链接是用于torch-th 的,它是torch 的相当古老的部分。它适用于 libtroch C++ 吗?
    猜你喜欢
    • 1970-01-01
    • 2013-09-10
    • 2020-12-09
    • 2020-01-07
    • 2020-06-08
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多