在C ++中的CMakeLists.txt:10(find_package)处发生炬管CMake错误

时间:2018-12-17 16:30:49

标签: c++ cmake torch

我试图通过Eclipse C++创建一个CMake的{​​{1}}项目。我运行torch/torch.h创建一个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.cmake文件;不过,我在torch-config.cmake拥有 TorchConfig.cmake。通过测试以下各项,我将相应的路径添加到/home/afshin/libtorch/share/cmake/Torch文件中:

CMakeLists.txt

但是,它没有帮助,我仍然遇到相同的错误。 感谢您的帮助或评论。

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

enter image description here

谢谢, 苦参碱

2 个答案:

答案 0 :(得分:1)

以下经过修改的CMakeLists.txt文件在没有明显丢失的TorchConfig.cmake的情况下仍有效(在vcpkg安装here中也丢失)。我建议使用Microsoft的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)

答案 1 :(得分: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也可以通过以下设置解决问题:

enter image description here

依次点击configuregenerate。 最后,我通过选择Eclipse将项目导入到Makefile Project With Existing Code中。此代码已编译并成功构建。