Problems using LibTorch

username: G.M
cooked: <p>I’m new to LibTorch and am currently learning how to use it. I have a few questions.</p>
<ol>
<li>I want to be clear which directory should I add as a dependency, should I add <code>libtorch/include</code> or <code>libtorch\include\torch\csrc\api\include</code>?</li>
<li>In multiple tutorials on <a href="http://pytorch.org" rel="nofollow noopener">pytorch.org</a> I saw multiple “one-stop” header, that’s very confusing.<br>
Over <a href="https://pytorch.org/tutorials/advanced/cpp_extension.html#" rel="nofollow noopener">here</a>, it’s suggesting <code>#include &lt;torch/extension.h&gt;</code>, which doesn’t work for me when I add <code>libtorch/include</code> as dependency.<br>
Over <a href="https://pytorch.org/tutorials/advanced/cpp_export.html#a-minimal-c-application" rel="nofollow noopener">here</a>,  it suggests <code>include&lt;torch/script.h&gt;</code>.<br>
And at <a href="https://pytorch.org/tutorials/advanced/cpp_frontend.html" rel="nofollow noopener">here</a>, it has a <code>include&lt;torch/torch.h&gt;</code> which I can’t even include from <code>libtorch/include</code>.</li>
</ol>
<p>Can anyone give me any explanations, btw I’m using visual studio.<br>
Thank you very much.</p>

username: Mikele
cooked: <p>hey,<br>
first of all if you are using cmakelists you should use find_package, e.g. if libtorch is in the subfolder deps then:</p>
<pre><code class="lang-auto">set(CMAKE_PREFIX_PATH deps/libtorch)
find_package(Torch REQUIRED)
</code></pre>
<p>This will define TORCH_INCLUDE_DIRS which are both of the folders you mentioned and should be included in your target!<br>
From the torchconfig:</p>
<pre><code class="lang-auto">  set(TORCH_INCLUDE_DIRS
    ${TORCH_INSTALL_PREFIX}/include
    ${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
</code></pre>
<p>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.</p>
<p>From my experience torch.h gives you all the headers you will need!<br>
But for completeness:<br>
extension.h is just</p>
<pre><code class="lang-auto">#pragma once

// All pure C++ headers for the C++ frontend.
#include &lt;torch/all.h&gt;
// Python bindings for the C++ frontend (includes Python.h).
#include &lt;torch/python.h&gt;
</code></pre>
<p>torch.h is</p>
<pre><code class="lang-auto">#pragma once

#include &lt;torch/all.h&gt;

#ifdef TORCH_API_INCLUDE_EXTENSION_H
#include &lt;torch/extension.h&gt;

#endif // defined(TORCH_API_INCLUDE_EXTENSION_H)
</code></pre>
<p>so arguably torch.h will provide all headers if the macro is set. Hope this helps.</p>

username: G.M
cooked: <p>thanks, I’ll try it.</p>

username: G.M
cooked: <p>Another question, do u have any experience of using Pybind11 with it?</p>

username: Mikele
cooked: <p>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.</p>

username: G.M
cooked: <p>alright thx <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: uniquenospacesshort
cooked: <p>Hi <a class="mention" href="/u/mikele">@Mikele</a>,</p>
<p>I tried your sollution, but I got “no such file or directory” when I tried to include &lt;torch/torch.h&gt;. Any ideas to what the issue could be?</p>
<p>I’ve pasted my CMake code below:</p>
<pre><code class="lang-auto">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)
</code></pre>

username: Mikele
cooked: <p>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.</p>
<p>However, if this is all of your cmake code, then you’re lacking an include and a linking of the torch directories:<br>
target_include_directories(example-app PUBLIC ${TORCH_INCLUDE_DIRS})<br>
target_link_libraries(example-app PUBLIC ${TORCH_LIBRARIES})</p>