Search code examples
c++cmaketorch

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


Solution

  • 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.