Afshin Oroojlooy
Afshin Oroojlooy

Reputation: 1434

Torch CMake Error at CMakeLists.txt:10 (find_package) in C++

I am trying to create a Eclipse C++ project by CMake which calls torch/torch.h . I run cmake -G "Eclipse CDT4 - Unix Makefiles" ./ to create a Eclipse project, but I get this error:

-- 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!

In which CMakeLists.txt is located in the current directory that has:

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)

Apparently, it cannot find TorchConfig.cmake and torch-config.cmake files; although, I have TorchConfig.cmake in /home/afshin/libtorch/share/cmake/Torch. I added the corresponding path into the CMakeLists.txt file by testing each of:

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}")

But, it did not help and I still get same error. I appreciate any help or comments.

I also tried the cmake-gui and I get same error:

enter image description here

Thanks, Afshin

Upvotes: 1

Views: 8095

Answers (2)

Liviu
Liviu

Reputation: 1917

The following modified CMakeLists.txt file works without the apparently missing TorchConfig.cmake (also missing in the vcpkg installation here). I recommend Microsoft's vcpkg for cross-platform packages (c++ libraries) management (usage here). But the code is vcpkg independent: one can set TORCH_BASE_PATH (or Torch_INCLUDE_DIR and Torch_LIBRARIES) to the proper paths.

#[[
    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)

Upvotes: 1

Afshin Oroojlooy
Afshin Oroojlooy

Reputation: 1434

I was able to solve the problem by editing the CMakeLists.txt as:

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)

Alternatively, using the cmake-gui also I was able to solve the problem by the following setting:

enter image description here

by hitting configure, and then generate. Finally, I imported this project into Eclipse by selecting Makefile Project With Existing Code. This code is compiled and built successfully.

Upvotes: 4

Related Questions