Torch_dir not found

username: ilhem
cooked: <p>I use the cmaker and the cmakerlists.txt and I do the configuration but an error which appears to me is torch_dir not found. what am i doing?</p>

username: ptrblck
cooked: <p>Are you trying to build a libtorch application? If so, could you post the <code>CMakeLists.txt</code> as well as your current file structure?</p>

username: ilhem
cooked: <p>I install the pytorch . this is my CMakeLists.txt:</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION "3.18.0")
project(example-app)

find_package(Torch REQUIRED)
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)

# The following code block is suggested to be used on Windows.
# According to https://github.com/pytorch/pytorch/issues/25457,
# the DLLs need to be copied to avoid memory errors.
if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET example-app
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $&lt;TARGET_FILE_DIR:example-app&gt;)
endif (MSVC)
</code></pre>
<p>C:\Users\hp\Desktop\example-app\ CMakeLists.txt</p>

username: peterjc123
cooked: <p>Have you correctly passed <code>CMAKE_PREFIX_PATH</code> through command?</p>

username: ilhem
cooked: <p>i install pythorch via conda but  when i use this command it doesn’t work</p>
<blockquote>
<p>cmake -DCMAKE_PREFIX_PATH=<code>python -c 'import torch;print(torch.utils.cmake_prefix_path)'</code><br>
as well i use</p>
</blockquote>
<blockquote>
<p>cmake -DCMAKE_PREFIX_PATH=c:\hp\desktop\libtorch  t</p>
</blockquote>
<p>he same does not work</p>

username: ilhem
cooked: <p>do you need the libtorch and example-app folder in the same folder?<br>
how to comment on the installation torch?</p>

username: ilhem
cooked: <p>my error in this method</p>
<blockquote>
<p>find_package(Torch REQUIRED)</p>
</blockquote>

username: FabianSchuetze
cooked: <p>I am on a linux machine so the way I do it might be different from you. Nevertheless, one way to add the torch_lib_dir on linux is as follows</p>
<ol>
<li>If you are in the build directory (one down from the cmake file), type <code>ccmake ..</code> which open the cmake gui.</li>
<li>Then, you will see a line stating: TORCH_DIR. You can set this to the path containing <code>libtorch/share/cmake/Torch</code>.  In my case this is <code>home/fabian/torch_cpp/libtorch/share/cmake/Torch</code>
</li>
<li>Configure and Generate the cmake file (press c and g)</li>
</ol>
<p>With these steps I could resolve the error <code>torch_dir not found</code>. Do these steps work for you?</p>

username: peterjc123
cooked: <p>Are you using the Release or the Debug version? You could try the following commands.</p>
<pre><code class="lang-auto">pip install ninja
# Activate the VC environment
for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -version [15^,17^) -products * -latest -property installationPath`) do call "%i\VC\Auxiliary\Build\vcvarsall.bat" x64
set CC=cl.exe
set CXX=cl.exe
cmake -GNinja  -DCMAKE_PREFIX_PATH=c:\hp\desktop\libtorch -DCMAKE_BUILD_TYPE=Release ..
</code></pre>

username: ilhem
cooked: <p>I don’t understand you <img src="https://discuss.pytorch.org/images/emoji/apple/frowning_face.png?v=9" title=":frowning_face:" class="emoji" alt=":frowning_face:"> but step 1 : I hit the prompt cmake gui<br>
step 2 : i don’t see torch-dir in the cmake gui</p>

username: ilhem
cooked: <p><a class="mention" href="/u/peterjc123">@peterjc123</a> i install ninja but the same error</p>

username: peterjc123
cooked: <aside class="quote no-group" data-username="ilhem" data-post="3" data-topic="86208">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/90ced4/48.png" class="avatar"> ilhem:</div>
<blockquote>
<p><code>TORCH_INSTALL_PREFIX</code></p>
</blockquote>
</aside>
<p>Could you please show me the directory structure of your libtorch folder using <code>tree "c:\hp\desktop\libtorch"</code>?</p>

username: ilhem
cooked: <aside class="quote no-group" data-username="peterjc123" data-post="9" data-topic="86208">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/peterjc123/48/2790_2.png" class="avatar"> peterjc123:</div>
<blockquote>
<pre><code class="lang-auto">cmake -GNinja  -DCMAKE_PREFIX_PATH=c:\hp\desktop\libtorch -DCMAKE_BUILD_TYPE=Release ..
</code></pre>
</blockquote>
</aside>
<p>C:\Users\hp\Desktop\libtorch</p>

username: ilhem
cooked: <p>C:\Users\hp\Desktop\libtorch</p>

username: peterjc123
cooked: <p>Is this the output of the command? If yes, then it is an empty folder, please download the LibTorch binary at <a href="https://pytorch.org/" rel="nofollow noopener">https://pytorch.org/</a> and extract them into this folder.</p>

username: ilhem
cooked: <p>i install pytorch via conda in prompt anaconda is that wrong?</p>

username: peterjc123
cooked: <p>That is okay, but you’ll need to find out the directory of that installation.</p>

username: ilhem
cooked: <p><a class="mention" href="/u/peterjc123">@peterjc123</a> successfully work thank you <img src="https://discuss.pytorch.org/images/emoji/apple/smiling_face_with_three_hearts.png?v=9" title=":smiling_face_with_three_hearts:" class="emoji" alt=":smiling_face_with_three_hearts:"></p>