Compiling example-app with CMake and Visual Studio on Windows

Hi,

I’m trying to compile the example-app from the libtorch tutorial using Visual Studio 16 (2019).

The CMakeLists.txt file is the following:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

set(CMAKE_PREFIX_PATH "C:\\Users\\caill\\libtorch\\")

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)

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

When building the example, I get the following error:

C:\Users\caill\source\Repos\caillonantoine\example-app\ninja : error : 'torch-NOTFOUND', needed by 'example-app.exe', missing and no known rule to make it

I’ve been trying to find a solution to this problem online, without success. Do you by any chance know how to fix this problem? Thanks!

Could you please clean the cache and build the project again?

Hi,
I don’t know a nice way how to solve this problem, but here is a dirty solution (and it works!).

Find file ‘biuld.ninja’ . For VS2019 it’s in ‘[your_project_dir]\out\build\x64-Release’ directory.
For VS2017 I’ve found it in ‘[your_win_home_dir]\CMakeBuilds\4483b75d-3038-b138-ad88-323c5cdc1522\build\x64-Release’ directory (or Debug if you trying it).

Then you have manually replace in this text file ‘torch-NOTFOUND’ string to full path to your torch.lib (like ‘C$:\libtorch\lib\torch.lib’ (don’t ask me what ‘$’ mean after C, I don’t know. It all experimental, but it work for me.)
Save ‘build.ninja’ file and now you can build and run your project.

Wonderful, isn’t it?

i so had this problem and solved it . delete catch and clean all in visual studio and delete cmake_minimum_required(VERSION 3.0 FATAL_ERROR) from CMakeLists.txt. and then generate catch and build project , then works as well.