Cmake problem minimal example for c++ frontend

heitaum: <p>Hello everyone !</p>
<p>I tried to follow this installation tuto step by step : <a href="https://pytorch.org/cppdocs/installing.html" class="inline-onebox" rel="noopener nofollow ugc">Installing C++ Distributions of PyTorch — PyTorch main documentation</a></p>
<p>I reached the point where I use the cmake command, but it won’t work. I made a folder “example-app” which contains:</p>
<ul>
<li>“example-app.cpp” file</li>
<li>“MakeList.txt” file</li>
<li>“libtorch” folder</li>
<li>“build” folder</li>
</ul>
<p>using cmake from the build folder (as written in the tutorial ?) is useless, it just tells me there’s no makelist file in the folder. I of course did my following tests from the example-app folder. (It’s the first time I use Cmake, so I’m a bit lost, but I assumed it was the thing to do)</p>
<p>so, here were my first results :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/0/0be960b2315caa3af46aaaa6d4f53c8c826ec83a.png" data-download-href="https://discuss.pytorch.org/uploads/default/0be960b2315caa3af46aaaa6d4f53c8c826ec83a" title="Capture%20du%202019-05-03%2017-27-27"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/0/0be960b2315caa3af46aaaa6d4f53c8c826ec83a_2_682x500.png" alt="Capture%20du%202019-05-03%2017-27-27" data-base62-sha1="1HngdIcKAOw0paVRP781cbZHUAa" width="682" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/0/0be960b2315caa3af46aaaa6d4f53c8c826ec83a_2_682x500.png, https://discuss.pytorch.org/uploads/default/original/2X/0/0be960b2315caa3af46aaaa6d4f53c8c826ec83a.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/0/0be960b2315caa3af46aaaa6d4f53c8c826ec83a.png 2x" data-dominant-color="3C1931"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Capture%20du%202019-05-03%2017-27-27</span><span class="informations">866×634 106 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>afterward, I added<br>
set(TORCH_LIBRARY /home/mathieu/Thales/example-app/libtorch/lib/libtorch.so) to my MakeList.txt, which looked like this :</p>
<pre><code class="lang-auto">make_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

set(TORCH_LIBRARY /home/mathieu/Thales/example-app/libtorch/lib/libtorch.so)
find_package(Torch REQUIRED)

add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 11)
</code></pre>
<p>new result was such :</p>
<pre><code class="lang-auto">cmake -DCMAKE_PREFIX_PATH=$PWD/libtorch
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Found torch: /home/mathieu/Thales/example-app/libtorch/lib/libtorch.so  
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
torch;caffe2_library;C10_LIBRARY
    linked by target "example-app" in directory /home/mathieu/Thales/example-app

-- Configuring incomplete, errors occurred!
See also "/home/mathieu/Thales/example-app/CMakeFiles/CMakeOutput.log".
See also "/home/mathieu/Thales/example-app/CMakeFiles/CMakeError.log".

</code></pre>
<p>The others things I tried, but it gave me the exact same results :</p>
<blockquote>
<p>putting the MakeList.txt file in the build directory and executing it from there.</p>
</blockquote>
<blockquote>
<p>adding “set(Torch_DIR /home/mathieu/Thales/example-app/libtorch/share/cmake/Torch)” before the find_package in MakeList.txt</p>
</blockquote>
<blockquote>
<p>exchanging back and forth  “/home/mathieu/Thales/example-app” and $PWD in various places again and again</p>
</blockquote>
<blockquote>
<p>voodoo magic</p>
</blockquote>
<blockquote>
<p>adding “set(CMAKE_PREFIX_PATH /home/mathieu/Thales/example-app/libtorch)” before, then after the find_package</p>
</blockquote>
<p>I’m on ubuntu 18.04, using cmake 3.10.2. gcc is 7.3</p>
<p>I’ve been looking for answers on the net for the whole day and even tried on a different computer. I’m running out of ideas, so I hope to find some help here ^^</p>
<p>If otherwise someone have found another tutorial with more details and which works, That would be nice.</p>
<p>Have a nice day !<br>
Mathieu</p>

heitaum: <p>I found a solution : for some reasons the function</p>
<blockquote>
<p>find_library(TORCH_LIBRARY torch PATHS “${TORCH_INSTALL_PREFIX}/lib”)</p>
</blockquote>
<p>in the torchConfig.cmake file wasn’t working.<br>
I just added</p>
<blockquote>
<p>set(TORCH_LIBRARY, “${TORCH_INSTALL_PREFIX}/lib/libtorch.so”)</p>
</blockquote>
<p>below and it worked like a charm.</p>

heitaum: <p>What ? I haven’t withdrawn anything, did I ? o_O</p>

pascal.soveaux: <p>I did. My answer was incorrect.</p>

heitaum: <p>Hello Pascal,</p>
<p>thanks for your e-mail and for your help.</p>
<p>libtorch now works fine here !</p>
<p>Have a nice evening,<br>
Mathieu Hernandez</p>

novioleo: <p>cross compile to win64:</p>
<pre><code class="lang-auto">cmake -DCMAKE_PREFIX_PATH="/work/libtorch_win;/work/opencv-3.4.5/build" -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=/work/mingw-w64-x86_64.cmake -DTORCH_LIBRARY="/work/libtorch_win/lib/torch.dll" -DC10_LIBRARY="/work/libtorch_win/lib/c10.dll" ..
</code></pre>

wudi: <p>Hi, I had the same problem, and how did you solve the problem? Thanks:blush:</p>

heitaum: <p>I received this email from pascal (see above) :</p>
<blockquote>
<p>Hello Mathieu,</p>
<p>In your cmakelists.txt, add</p>
<pre><code class="lang-auto">set(CMAKE_PREFIX_PATH  ${Torch_DIR}) 
</code></pre>
<p>or (which I find not more convenient)</p>
<pre><code class="lang-auto">cd build
cmake -DCMAKE_PREFIX_PATH=/home/mathieu/Thales/example-app/libtorch ..
</code></pre>
<p>cmake is not looking for the .so file, refer to cmake find_package for more info.</p>
</blockquote>
<p>I did what he asked me and it worked <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

neolei: <p>i met same problem.<br>
my CMAKElists.txt</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

list(APPEND CMAKE_PREFIX_PATH "D:/pp_extension/libtorch-win-shared-with-deps-debug-1.5.1+cpu/libtorch")

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app main.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_if_different
            ${TORCH_DLLS}
            $&lt;TARGET_FILE_DIR:example-app&gt;)
endif (MSVC)
</code></pre>
<p>but it did not work</p>

neolei: <p>Is there any solution？</p>