Torch_dir not found

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?

Are you trying to build a libtorch application? If so, could you post the CMakeLists.txt as well as your current file structure?

I install the pytorch . this is my CMakeLists.txt:

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

C:\Users\hp\Desktop\example-app\ CMakeLists.txt

Have you correctly passed CMAKE_PREFIX_PATH through command?

i install pythorch via conda but when i use this command it doesn’t work

cmake -DCMAKE_PREFIX_PATH=python -c 'import torch;print(torch.utils.cmake_prefix_path)'
as well i use

cmake -DCMAKE_PREFIX_PATH=c:\hp\desktop\libtorch t

he same does not work

do you need the libtorch and example-app folder in the same folder?
how to comment on the installation torch?

my error in this method

find_package(Torch REQUIRED)

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

  1. If you are in the build directory (one down from the cmake file), type ccmake .. which open the cmake gui.
  2. Then, you will see a line stating: TORCH_DIR. You can set this to the path containing libtorch/share/cmake/Torch. In my case this is home/fabian/torch_cpp/libtorch/share/cmake/Torch
  3. Configure and Generate the cmake file (press c and g)

With these steps I could resolve the error torch_dir not found. Do these steps work for you?

Are you using the Release or the Debug version? You could try the following commands.

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

I don’t understand you :frowning_face: but step 1 : I hit the prompt cmake gui
step 2 : i don’t see torch-dir in the cmake gui

@peterjc123 i install ninja but the same error

Could you please show me the directory structure of your libtorch folder using tree "c:\hp\desktop\libtorch"?

C:\Users\hp\Desktop\libtorch

C:\Users\hp\Desktop\libtorch

Is this the output of the command? If yes, then it is an empty folder, please download the LibTorch binary at https://pytorch.org/ and extract them into this folder.

i install pytorch via conda in prompt anaconda is that wrong?

That is okay, but you’ll need to find out the directory of that installation.

@peterjc123 successfully work thank you :smiling_face_with_three_hearts: