Cmake List for Libtorch and ATen

BiggestZocker: <p>I’m trying to make a OBS plugin using Libtorch. My CMake List runs fine and everything works, including the use of <code>#include &lt;torch/torch.h&gt; </code>, but when I add <code>#include &lt;variable_factories.h&gt;</code> (which has <code>#include &lt;ATen_fwd.h&gt;</code> in it), I get</p>
<blockquote>
<p>D:\OBS Modding\obs-plugintemplate\src\tensor-handler.cpp(4): fatal error C1083: Datei (Include) kann nicht geöffnet werden: “ATen_fwd.h”: No such file or directory [D:\OBS Modding\obs-plugintemplate\build_x64\my-test-plugin.<br>
vcxproj]</p>
</blockquote>
<p>Here is my CmakeList.txt</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.24...3.26)

set(CMAKE_COMPILE_WARNING_AS_ERROR OFF)

include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/common/bootstrap.cmake" NO_POLICY_SCOPE)
include(CMakePrintHelpers)

project(${_name} VERSION ${_version})

option(ENABLE_FRONTEND_API "Use obs-frontend-api for UI functionality" ON)
option(ENABLE_QT "Use Qt functionality" OFF)

include(compilerconfig)
include(defaults)
include(helpers)



add_library(${CMAKE_PROJECT_NAME} MODULE)

find_package(libobs REQUIRED)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE OBS::libobs)

#if(ENABLE_FRONTEND_API)
find_package(obs-frontend-api REQUIRED)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE OBS::obs-frontend-api)
#endif()


#Cuda

set(CUDACXX "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8/bin")
include(CheckLanguage)
check_language(CUDA)
enable_language(CUDA)

cmake_print_variables(CMAKE_CXX_COMPILER_ID)
cmake_print_variables(CMAKE_CXX_COMPILER_VERSION)
cmake_print_variables(CUDA_TOOLKIT_ROOT)

find_package(CUDAToolkit REQUIRED)
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC CUDA::cudart)
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC CUDA::cuda_driver)


#Libtorch
list(APPEND CMAKE_PREFIX_PATH "D:/libtorch")


set(Torch_DIR D:/libtorch/share/cmake/Torch)

find_package(Torch REQUIRED CONFIG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE "${TORCH_LIBRARIES}")
if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET ${CMAKE_PROJECT_NAME}
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $&lt;TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}&gt;)
endif (MSVC)

#ATen
#I also tried with this block as uncommented, but it didn't change anything
#set(ATen_DIR D:/libtorch/share/cmake/Aten)
#find_package(ATen REQUIRED)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ATEN_CXX_FLAGS}")
#target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE "${ATEN_LIBRARIES}")

#set(Caffe2_DIR D:/libtorch/share/cmake/Caffe2)
#find_package(Caffe2 REQUIRED)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CAFFE2_CXX_FLAGS}")
#target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE "${Caffe2_LIBRARIES}")

#set(fbgemm_DIR D:/libtorch/share/cmake/fbgemm)
#find_library(fbgemm REQUIRED)
#target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE "${fbgemm_LIBRARIES}")

#set(fbjni_DIR D:/libtorch/share/cmake/fbjni)
#find_package(fbjni REQUIRED)
#target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE "${fbjni_LIBRARIES}")

#set(kineto_DIR D:/libtorch/share/cmake/kineto)
#find_package(kineto REQUIRED)
#target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE "${kineto_LIBRARIES}")


if(ENABLE_QT)
  find_package(Qt6 COMPONENTS Widgets Core)
  target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt6::Core Qt6::Widgets)
  target_compile_options(
    ${CMAKE_PROJECT_NAME} PRIVATE $&lt;$&lt;C_COMPILER_ID:Clang,AppleClang&gt;:-Wno-quoted-include-in-framework-header
                                  -Wno-comma&gt;)
  set_target_properties(
    ${CMAKE_PROJECT_NAME}
    PROPERTIES AUTOMOC ON
               AUTOUIC ON
               AUTORCC ON)
endif()

target_sources(${CMAKE_PROJECT_NAME} PRIVATE src/plugin-main.cpp src/frameGrabberFilter.cpp src/positioning-output.cpp src/tensor-handler.cpp)

set_target_properties_plugin(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME ${_name})


</code></pre>
<p>I also tried to find a way to add ATen seperately to my CMakeList.txt, but could’t make it work.</p>

BiggestZocker: <p>The problem were the imports. The correct imports are</p>
<pre><code class="lang-auto">#include &lt;ATen/core/ATen_fwd.h&gt;
#include &lt;torch/csrc/autograd/generated/variable_factories.h&gt;
</code></pre>
<p>It seems like the Libtorch headers are very well hidden. To find any import search the function (in C/C++ code aka with :: instead of .) on <a href="https://pytorch.org/" rel="noopener nofollow ugc">https://pytorch.org/</a>, then click on “defined in [file_name].h” and look under “Definition”. There is the full import path.</p>