Relative paths not recognized with CMake

Viewed 29

I'm new to C++ and I would like to use the Pytorch library in C++. The tutorial can be seen here https://pytorch.org/cppdocs/installing.html

I have an issue with my /home/local/CLionProjects/MachineAI/CMakeLists.txt file

cmake_minimum_required(VERSION 3.2)
project(MachineAI)

set(CMAKE_PREFIX_PATH ${CMAKE_SOURCE_DIR}/libtorch)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} "${TORCH_LIBRARIES}" )
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)

This produces a lot of errors but they mostly seem to have the same problem. It's not able to find a specific file. Here's an example:

CMake Error at libtorch/share/cmake/Caffe2/Caffe2Config.cmake:14 (include):
  include could not find requested file:
    public/utils.cmake

The error is suggesting the wrong path is provided. If I take a look inside the libtorch/share/cmake/Caffe2/Caffe2Config.cmake, the problem line is:

include("public/utils.cmake")

It cannot find the file even though the file is located at the correct relative path libtorch/share/cmake/Caffe2/public/utils.cmake

If I replace the path with an absolute path, the error goes away:

include("/home/local/CLionProjects/MachineAI/libtorch/share/cmake/Caffe2/public/utils.cmake")

Here is the screenshot of relevant files for better orientation:

enter image description here

I would like to know if there's a way to fix the CMakeLists.txt file at the top so that Caffe2Config.cmake will be able to find the files through the relative path.

0 Answers
Related