Where to find <torch/torch.h>?

Peter_Zheng: <p>I succeed in compiling the code with cmake directly, but when I use vscode for editing, it always get error message with &lt;torch/torch.h&gt;  not found. How can I get it?</p>

tom: <p>Hi,</p>
<p>you need to add the somewhat hidden <code>&lt;installation dir&gt;/include/torch/csrc/api/include</code> to the includes.<br>
If you create the CMakeLists.txt as in <a href="https://pytorch.org/cppdocs/installing.html" rel="nofollow noopener">the example</a>, the TorchConfig.cmake should set it up for you up when running <code>find_package(Torch REQUIRED)</code>.</p>
<p>Best regards</p>
<p>Thomas</p>

Peter_Zheng: <p>It’s works!!<br>
Thanks!!<img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=9" title=":grinning:" class="emoji" alt=":grinning:"></p>

Roger_Aylagas: <p>Hi I am starting using Torch in c++ and I do not know what are you refering as adding the folder to the includes. Sorry if this is a very basic question.</p>

Hartbook: <p>It depends on how you compile your software.</p>
<p>If you use cmake you can do this in your CMakelist</p>
<pre><code class="lang-auto">find_package(Torch REQUIRED)
include_directories(SYSTEM ${TORCH_INCLUDE_DIRS})
</code></pre>

ilhem: <p>hi, i create CMakeLists.txt as in <a href="https://pytorch.org/cppdocs/installing.html" rel="nofollow noopener">the example</a> and I do the configuration but appearing an error is : torch_dir not found ? what do i do for succeful compiling .</p>

naveen139: <p>Add this line to your project’s CMakeLists.txt file.</p>
<pre><code class="lang-auto">set(CMAKE_PREFIX_PATH "&lt;libtorch-path&gt;/share/cmake/Torch")
</code></pre>

ambiguity: <p>Hi,</p>
<p>After I linked <code>&lt;installation dir&gt;/include/torch/csrc/api/include</code>  to the includes, with command line  <code>sudo ln -s /usr/local/Cellar/libtorch/1.6.0_1/include/torch/csrc/api/include/torch torch</code> in the directory <code>/usr/local/include</code>, my vscode and ycm of vim still can’t work. Am I wrong?</p>

tom: <p>I’m not sure symlinking is a good way to go about it, as you need several directories to be set up that contain torch (i.e. torch/include and torch/csrc/api/include).</p>

DineshRajan_T: <p>Did this work for you <a class="mention" href="/u/naveen139">@naveen139</a>?</p>

naveen139: <p>Ignore that, that was for some workaround to get early alpha package working. In recent versions, you don’t need to do that. You can simply do below:</p>
<pre><code class="lang-auto">list(APPEND CMAKE_PREFIX_PATH "&lt;libtorch-path&gt;")
find_package(Torch REQUIRED)
</code></pre>

DineshRajan_T: <p>Should I add it in the CMakeLists.txt file or in TorchConfig.cmake?</p>

naveen139: <p>Yes, in your root project folder’s <code>CMakeLists.txt</code>. Don’t make any changes to <code>TorchConfig.cmake</code></p>
<p>but, if you are encountering an error <code>missing dll(s)</code> on windows, here is an workaround.</p>
<pre><code class="lang-auto">file(GLOB TORCH_DLLS ${TORCH_INSTALL_PREFIX}/lib/*.dll)
 add_custom_command(TARGET &lt;exe-name&gt; POST_BUILD
   COMMAND ${CMAKE_COMMAND} -E copy
   ${TORCH_DLLS}
   ${CMAKE_CURRENT_BINARY_DIR}
   DEPENDS &lt;exe-name&gt;
 )
</code></pre>
<p>this copies dlls into path of your executable.</p>

DineshRajan_T: <p>This is my CMakeLists.txt. I am still getting the same error, which is "fatal error C1083: Cannot open include file: ‘torch/torch.h’ "</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

find_package(Torch REQUIRED)
list(APPEND CMAKE_PREFIX_PATH "&lt;my-libtorch-path&gt;")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)

if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET example-app
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy
                     ${TORCH_DLLS}
                     ${CMAKE_CURRENT_BINARY_DIR}
                     DEPENDS example-app)
endif (MSVC)
</code></pre>

naveen139: <p>You forgot to add include paths.</p>
<pre><code class="lang-auto">include_directories(
  "${TORCH_INCLUDE_DIRS}"
)
</code></pre>

DineshRajan_T: <p>where should I include that? Can you be more specific? I am a beginner dealing with CMake files.</p>

naveen139: <p>Sure. Just add it in the same file, but before <code>add_executable</code>.</p>

DineshRajan_T: <p>Hi <a class="mention" href="/u/naveen139">@naveen139</a>, I installed the torch via pip. Now that how should I include the directories?</p>

naveen139: <p>I don’t understand what you are trying to achieve. Are you trying to use libtorch to be able to call from python?</p>

DineshRajan_T: <p>I am trying this basic one…</p>
<pre><code class="lang-auto">https://pytorch.org/cppdocs/installing.html
</code></pre>
<p>And I get the error <code>"fatal error C1083: Cannot open include file: 'torch/torch.h': No such file or directory "</code><br>
I know I am missing the torch.h and I don’t where should I add that.</p>