'torch/torch.h' file not found in header file

Viewed 1401

I'm new in Linux C++ programming. I have to use libtorch (pytorch C++ version), but I kept struggling with C++ and cmake problems. These problems really make me mad, and I need some help.

Here is a simple C++ project:

.
├── build.sh
├── CMakeLists.txt
├── load_data
│   ├── CMakeLists.txt
│   ├── include
│   │   └── load_data
│   │       └── load_data.hpp
│   └── src
│       └── load_data.cpp
└── src
    └── main.cpp

But I can't build it because load_data/include/load_data/load_data.hpp cannot #include <torch/torch.h>.

The build error message:

[build] /my/path/load_data/include/load_data/load_data.hpp:4:10: fatal error: 'torch/torch.h' file not found
[build] #include <torch/torch.h>
[build]          ^~~~~~~~~~~~~~~

However, if I move the #include <torch/torch.h> from the header file to the corresponding source file, the error message goes away. It makes me confused.

Files' contents:

  • load_data/include/load_data/load_data.hpp

    #pragma once
    
    #include <torch/torch.h>
    
    namespace load_data
    {
        void try_torch();
        // torch::Tensor rand_tensor();
    }
    
  • load_data/src/load_data.cpp

    # include "load_data/load_data.hpp"
    
    // If I remove it from load_data.hpp and use it here, the error goes away.
    // #include <torch/torch.h>
    
    namespace load_data {
        void try_torch(){
            torch::Tensor tensor = torch::rand({2, 3});
            std::cout << tensor << std::endl;
        }
    }
    
  • src/main.cpp

    #include <string>
    
    #include "load_data/load_data.hpp"
    
    int main(int argc, char **argv)
    {
        load_data::try_torch();
        return 0;
    }
    
  • CMakeLists.txt

    cmake_minimum_required (VERSION 3.21)
    
    set(CMAKE_CXX_COMPILER clang++-13)
    
    project(ldt-cpp-experiments VERSION 1.0.0 LANGUAGES CXX)
    
    
    find_package(Torch REQUIRED)
    message(STATUS "Torch_FOUND: ${Torch_FOUND}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
    
    add_subdirectory("load_data")
    
    
    set(EXECUTABLE_NAME main)
    
    add_executable(${EXECUTABLE_NAME} src/main.cpp)
    
    
    target_link_libraries(${EXECUTABLE_NAME}
        PRIVATE load_data
    )
    
    set_target_properties(${EXECUTABLE_NAME} PROPERTIES
      CXX_STANDARD 20
      CXX_STANDARD_REQUIRED YES
    )
    
    
  • load_data/CMakeLists.txt

    cmake_minimum_required(VERSION 3.21)
    
    project (load_data VERSION 1.0.0 LANGUAGES CXX)
    
    
    find_package(Torch REQUIRED)
    message(STATUS "Torch_FOUND: ${Torch_FOUND}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
    
    
    set(LIB_NAME "load_data")
    
    add_library(${LIB_NAME}
        SHARED 
    )
    
    target_sources(${LIB_NAME}
        PRIVATE
            ${PROJECT_SOURCE_DIR}/src/load_data.cpp
    )
    
    target_sources(${LIB_NAME}
        PRIVATE
            ${PROJECT_SOURCE_DIR}/src/load_data.cpp
            ${PROJECT_SOURCE_DIR}/include/load_data/load_data.hpp
    )
    
    target_include_directories(${LIB_NAME}
        PUBLIC 
            ${PROJECT_SOURCE_DIR}/include
    )
    
    target_link_libraries(${LIB_NAME}
        PRIVATE
            ${TORCH_LIBRARIES}
    )
    
    set_target_properties(${LIB_NAME} PROPERTIES
      CXX_STANDARD 20
      CXX_STANDARD_REQUIRED YES
    )
    
    
  • build.sh

    cmake -S . -B build
    
0 Answers
Related