Reputation:
I want to include the pytorch C++ API to the large C++ software I am working on.
For legacy reasons, I must use find_package
and the associated find_path
and find_library
functions, instead of the proposed target_link_libraries
.
Here's my FindTORCH.cmake :
include( FindPackageHandleStandardArgs )
find_path( TORCH_INCLUDE_DIR torch/torch.h
PATHS
/path/to/libtorch/include/torch/csrc/api/include/
NO_DEFAULT_PATH )
find_library( TORCH_LIBRARIES libtorch.so
PATHS
/path/to/libtorch/lib/
NO_DEFAULT_PATH )
FIND_PACKAGE_HANDLE_STANDARD_ARGS( TORCH REQUIRED_VARS TORCH_INCLUDE_DIR TORCH_LIBRARIES )
if ( TORCH_FOUND )
message( STATUS "Torch found" )
endif( TORCH_FOUND )
mark_as_advanced( TORCH_LIBRARIES TORCH_INCLUDE_DIR )
At the compilation, the torch files are found and I can include <torch/torch.h>
in a random .cxx in the project.
However, if I add to the .cxx :
torch::Tensor tensor = torch::rand({2, 3});
cout << tensor << std::endl;
Then I cannot compile anymore and I get the following error :
/path/to/libtorch/include/torch/csrc/api/include/torch/utils.h:4:10: fatal error: ATen/record_function.h: No such file or directory
#include <ATen/record_function.h>
^~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
I'm working Ubuntu 18, C++ 14, and the cmake version is 3.10.2 .
Thanks in advance
Upvotes: 0
Views: 2183
Reputation: 41750
Torch exposes its own targets. To use them effectively, simply remove FindTORCH.cmake
from your project, and add /path/to/libtorch/
to your prefix path:
cmake_minimum_required(VERSION 3.19) # or whatever version you use
project(your-project CXX)
list(APPEND CMAKE_PREFIX_PATH "/path/to/libtorch/")
find_package(Torch REQUIRED CONFIG) # this ensure it find the file provided by pytorch
add_executable(your-executable main.cpp)
target_link_libraries(your-executable PUBLIC torch::Tensor)
If you really insist to use your own FindTorch.cmake
instead of the correct one, you can modify it to create an imported target that you will then link:
You can change your find module very slightly to gain a modern CMake interface from it:
include( FindPackageHandleStandardArgs )
find_path( TORCH_INCLUDE_DIR torch/torch.h
PATHS
/path/to/libtorch/include/torch/csrc/api/include/
NO_DEFAULT_PATH )
find_library( TORCH_LIBRARIES libtorch.so
PATHS
/path/to/libtorch/lib/
NO_DEFAULT_PATH )
FIND_PACKAGE_HANDLE_STANDARD_ARGS( TORCH REQUIRED_VARS TORCH_INCLUDE_DIR TORCH_LIBRARIES )
if ( TORCH_FOUND )
message( STATUS "Torch found" )
add_library(torch::Tensor SHARED IMPORTED) # mimic the names from pytorch maintainers
set_target_properties(torch::Tensor
PROPERTIES
IMPORTED_LOCATION "${TORCH_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${TORCH_INCLUDE_DIR}"
# on windows, set IMPORTED_IMPLIB to the .lib
)
endif( TORCH_FOUND )
mark_as_advanced( TORCH_LIBRARIES TORCH_INCLUDE_DIR )
Then, in your main CMake file, you can use the imported target like any other targets:
find_package(Torch REQUIRED)
add_executable(your-executable main.cpp)
target_link_libraries(your-executable PUBLIC torch::Tensor)
Upvotes: 1