Cmake problem minimal example for c++ frontend

Hello everyone !

I tried to follow this installation tuto step by step : https://pytorch.org/cppdocs/installing.html

I reached the point where I use the cmake command, but it won’t work. I made a folder “example-app” which contains:

  • “example-app.cpp” file
  • “MakeList.txt” file
  • “libtorch” folder
  • “build” folder

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)

so, here were my first results :

afterward, I added
set(TORCH_LIBRARY /home/mathieu/Thales/example-app/libtorch/lib/libtorch.so) to my MakeList.txt, which looked like this :

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)

new result was such :

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".

The others things I tried, but it gave me the exact same results :

putting the MakeList.txt file in the build directory and executing it from there.

adding “set(Torch_DIR /home/mathieu/Thales/example-app/libtorch/share/cmake/Torch)” before the find_package in MakeList.txt

exchanging back and forth “/home/mathieu/Thales/example-app” and $PWD in various places again and again

voodoo magic

adding “set(CMAKE_PREFIX_PATH /home/mathieu/Thales/example-app/libtorch)” before, then after the find_package

I’m on ubuntu 18.04, using cmake 3.10.2. gcc is 7.3

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 ^^

If otherwise someone have found another tutorial with more details and which works, That would be nice.

Have a nice day !
Mathieu

I found a solution : for some reasons the function

find_library(TORCH_LIBRARY torch PATHS “${TORCH_INSTALL_PREFIX}/lib”)

in the torchConfig.cmake file wasn’t working.
I just added

set(TORCH_LIBRARY, “${TORCH_INSTALL_PREFIX}/lib/libtorch.so”)

below and it worked like a charm.

What ? I haven’t withdrawn anything, did I ? o_O

I did. My answer was incorrect.

Hello Pascal,

thanks for your e-mail and for your help.

libtorch now works fine here !

Have a nice evening,
Mathieu Hernandez

cross compile to win64:

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" ..

Hi, I had the same problem, and how did you solve the problem? Thanks:blush:

I received this email from pascal (see above) :

Hello Mathieu,

In your cmakelists.txt, add

set(CMAKE_PREFIX_PATH  ${Torch_DIR}) 

or (which I find not more convenient)

cd build
cmake -DCMAKE_PREFIX_PATH=/home/mathieu/Thales/example-app/libtorch ..

cmake is not looking for the .so file, refer to cmake find_package for more info.

I did what he asked me and it worked :slight_smile:

1 Like

i met same problem.
my CMAKElists.txt

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}
            $<TARGET_FILE_DIR:example-app>)
endif (MSVC)

but it did not work

Is there any solution?