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!

Has anyone managed to make this work? Using the JUCE CMake example on Windows 10 / VS2019, I tried the following with no luck.

In CMakeLists.txt

  • added
list(APPEND CMAKE_PREFIX_PATH "<my_path_to>/libtorch-win-shared-with-deps-debug-1.12.0+cpu/libtorch")

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
  • modified target_link_libraries():
target_link_libraries(AudioPluginExample
    PRIVATE
        # AudioPluginData           # If we'd created a binary data target, we'd link to it here
        juce::juce_audio_utils
        "${TORCH_LIBRARIES}"
    PUBLIC
        juce::juce_recommended_config_flags
        juce::juce_recommended_lto_flags
        juce::juce_recommended_warning_flags
)
  • added at the end:
if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET AudioPluginExample
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $<TARGET_FILE_DIR:AudioPluginExample>)
endif (MSVC)

It compiles fine, but upon trying to open the standalone, I get the usual “torch_cpu.dll not found”. If I move all the libtorch .dll files in the same directory as the executable, it opens just fine. But the VST3 obviously won’t open.

Any help much appreciated.

On windows you would put asmjit.dll, c10.dll, fbgemm.dll, libiomp5md.dll, torch.dll, torch_cpu.dll, uv.dll (and required vcruntimes) into the folder where your vst dynamic library file is.

Now the trick on windows is that by default, loading a dll is relative to where the executable is (the host application). You may have to explicitly add your vst folder to the DLL search path: SetDllDirectoryA function (winbase.h) - Win32 apps | Microsoft Learn

I believe you can just add all DLLs to C:\Windows\system32.

I’ve never been sure if that is “proper”, but it sure beats the heck out of trying to modify the DirectoryA path for every DAW.