Using LibTorch with JUCE

Hi, I’m trying to create a plugin in JUCE which uses pytorch. The C++ distribution of pytorch, ‘libtorch’, is described here:
https://pytorch.org/cppdocs/installing.html

What I’m struggling with is how to include the libtorch library in my JUCE project. I have successfully included other third party libraries, by adding the library directory to the projucer and using an include statement on the header file. However, for libtorch there doesn’t seem to be a header file that I can just include, the recommended installation process involves using CMake. Is there a way I can use this library within projucer/xcode?

Thanks

Is it too late to switch from Projucer to CMake as your build tool? You can use this example JUCE-CMake plugin project as a template, then add the following lines to CMakeLists.txt (taken directly from the libtorch installation link you included)

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

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)
2 Likes

Thanks, I will have a go at this. I didn’t realise there were CMake examples available!