Problems using LibTorch

I’m new to LibTorch and am currently learning how to use it. I have a few questions.

  1. I want to be clear which directory should I add as a dependency, should I add libtorch/include or libtorch\include\torch\csrc\api\include?
  2. In multiple tutorials on pytorch.org I saw multiple “one-stop” header, that’s very confusing.
    Over here, it’s suggesting #include <torch/extension.h>, which doesn’t work for me when I add libtorch/include as dependency.
    Over here, it suggests include<torch/script.h>.
    And at here, it has a include<torch/torch.h> which I can’t even include from libtorch/include.

Can anyone give me any explanations, btw I’m using visual studio.
Thank you very much.

hey,
first of all if you are using cmakelists you should use find_package, e.g. if libtorch is in the subfolder deps then:

set(CMAKE_PREFIX_PATH deps/libtorch)
find_package(Torch REQUIRED)

This will define TORCH_INCLUDE_DIRS which are both of the folders you mentioned and should be included in your target!
From the torchconfig:

  set(TORCH_INCLUDE_DIRS
    ${TORCH_INSTALL_PREFIX}/include
    ${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)

If you are intent on using visual studio id recommend to either use it with cmakelists or go through the cmake torchconfig to find all the relevant things to add.

From my experience torch.h gives you all the headers you will need!
But for completeness:
extension.h is just

#pragma once

// All pure C++ headers for the C++ frontend.
#include <torch/all.h>
// Python bindings for the C++ frontend (includes Python.h).
#include <torch/python.h>

torch.h is

#pragma once

#include <torch/all.h>

#ifdef TORCH_API_INCLUDE_EXTENSION_H
#include <torch/extension.h>

#endif // defined(TORCH_API_INCLUDE_EXTENSION_H)

so arguably torch.h will provide all headers if the macro is set. Hope this helps.

thanks, I’ll try it.

Another question, do u have any experience of using Pybind11 with it?

Unfortunately I don’t. But if your question of the thread is answered, I would recommend to accept this one’s solution and open up a new thread for specific questions about pybind.

1 Like

alright thx :slight_smile:

Hi @Mikele,

I tried your sollution, but I got “no such file or directory” when I tried to include <torch/torch.h>. Any ideas to what the issue could be?

I’ve pasted my CMake code below:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)


set(CMAKE_PREFIX_PATH /home/magnus/pytorch/torch)
find_package(Torch REQUIRED)
set(TORCH_INCLUDE_DIRS
    ${TORCH_INSTALL_PREFIX}/include
    ${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
add_executable(example-app example-app.cpp)

##target_link_libraries(example-app "/home/magnus/pytorch/build_libtorch/build/lib/libtorch.so")
set_property(TARGET example-app PROPERTY CXX_STANDARD 11)

You don’t need to set the torch include dirs yourself. If cmake finds the torch package then its configuration will set those for you.

However, if this is all of your cmake code, then you’re lacking an include and a linking of the torch directories:
target_include_directories(example-app PUBLIC ${TORCH_INCLUDE_DIRS})
target_link_libraries(example-app PUBLIC ${TORCH_LIBRARIES})