I succeed in compiling the code with cmake directly, but when I use vscode for editing, it always get error message with <torch/torch.h> not found. How can I get it?
Hi,
you need to add the somewhat hidden <installation dir>/include/torch/csrc/api/include
to the includes.
If you create the CMakeLists.txt as in the example, the TorchConfig.cmake should set it up for you up when running find_package(Torch REQUIRED)
.
Best regards
Thomas
It’s works!!
Thanks!!
Hi I am starting using Torch in c++ and I do not know what are you refering as adding the folder to the includes. Sorry if this is a very basic question.
It depends on how you compile your software.
If you use cmake you can do this in your CMakelist
find_package(Torch REQUIRED)
include_directories(SYSTEM ${TORCH_INCLUDE_DIRS})
hi, i create CMakeLists.txt as in the example and I do the configuration but appearing an error is : torch_dir not found ? what do i do for succeful compiling .
Add this line to your project’s CMakeLists.txt file.
set(CMAKE_PREFIX_PATH "<libtorch-path>/share/cmake/Torch")
Hi,
After I linked <installation dir>/include/torch/csrc/api/include
to the includes, with command line sudo ln -s /usr/local/Cellar/libtorch/1.6.0_1/include/torch/csrc/api/include/torch torch
in the directory /usr/local/include
, my vscode and ycm of vim still can’t work. Am I wrong?
I’m not sure symlinking is a good way to go about it, as you need several directories to be set up that contain torch (i.e. torch/include and torch/csrc/api/include).
Ignore that, that was for some workaround to get early alpha package working. In recent versions, you don’t need to do that. You can simply do below:
list(APPEND CMAKE_PREFIX_PATH "<libtorch-path>")
find_package(Torch REQUIRED)
Should I add it in the CMakeLists.txt file or in TorchConfig.cmake?
Yes, in your root project folder’s CMakeLists.txt
. Don’t make any changes to TorchConfig.cmake
but, if you are encountering an error missing dll(s)
on windows, here is an workaround.
file(GLOB TORCH_DLLS ${TORCH_INSTALL_PREFIX}/lib/*.dll)
add_custom_command(TARGET <exe-name> POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${TORCH_DLLS}
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS <exe-name>
)
this copies dlls into path of your executable.
This is my CMakeLists.txt. I am still getting the same error, which is "fatal error C1083: Cannot open include file: ‘torch/torch.h’ "
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)
find_package(Torch REQUIRED)
list(APPEND CMAKE_PREFIX_PATH "<my-libtorch-path>")
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)
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET example-app
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${TORCH_DLLS}
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS example-app)
endif (MSVC)
You forgot to add include paths.
include_directories(
"${TORCH_INCLUDE_DIRS}"
)
where should I include that? Can you be more specific? I am a beginner dealing with CMake files.
Sure. Just add it in the same file, but before add_executable
.
I don’t understand what you are trying to achieve. Are you trying to use libtorch to be able to call from python?
I am trying this basic one…
https://pytorch.org/cppdocs/installing.html
And I get the error "fatal error C1083: Cannot open include file: 'torch/torch.h': No such file or directory "
I know I am missing the torch.h and I don’t where should I add that.