Pytorch C++ API : CMake Issue
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 &quot;Torch found&quot; )
endif( TORCH_FOUND )

mark_as_advanced( TORCH_LIBRARIES TORCH_INCLUDE_DIR )


At the compilation, the torch files are found and I can include &lt;torch/torch.h&gt; in a random .cxx in the project.
However, if I add to the .cxx :
torch::Tensor tensor = torch::rand({2, 3});
cout &lt;&lt; tensor &lt;&lt; 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 &lt;ATen/record_function.h&gt;
          ^~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.


I'm working Ubuntu 18, C++ 14, and the cmake version is 3.10.2 .
Thanks in advance

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 &quot;/path/to/libtorch/&quot;)
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 &quot;Torch found&quot; )
    add_library(torch::Tensor SHARED IMPORTED) # mimic the names from pytorch maintainers
    set_target_properties(torch::Tensor 
    PROPERTIES
        IMPORTED_LOCATION &quot;${TORCH_LIBRARIES}&quot;
        INTERFACE_INCLUDE_DIRECTORIES &quot;${TORCH_INCLUDE_DIR}&quot;
        # 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)

