VST does not appear in DAW when using libtorch

Hi, I am trying to build an VST3 plugin that loads a TorchScript model. I am using CMAKE to build the project. Right now I am just building the default AudioPluginExample project from the JUCE repository. I have added the following code to the CMakeLists.txt file:

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
target_link_libraries(AudioPluginExample PRIVATE "${TORCH_LIBRARIES}")
set_property(TARGET AudioPluginExample 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 AudioPluginExample
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $<TARGET_FILE_DIR:AudioPluginExample>)
endif (MSVC)

The interesting thing is that I can load the project into a DAW (using Reaper) with no problems as long as I do not include the target_link_libraries(AudioPluginExample PRIVATE “${TORCH_LIBRARIES}”) line in CMakeLists.txt. As soon as I add that line, the plugin does not show up in the DAW any more. The build seems fine however, I do not get any errors while building the project. The commands I am using to build are:

cmake . -B cmake-build -DJUCE_BUILD_EXAMPLES=ON -DJUCE_BUILD_EXTRAS=ON -DCMAKE_PREFIX_PATH="D:\School\Grad School\Project\libtorch_debug\libtorch"
cmake --build cmake-build --target AudioPluginExample_VST3 --config Debug

Does anybody have any ideas as to what could be going wrong? Thanks!

Windows loads DLLs that are next to an executable when starting it, but that behavior doesn’t apply to plugins.

You have a few options:

  • build and link libtorch statically (so there is no DLL)
  • load the libtorch DLL at runtime (for instance using juce::DynamicLibrary)

Either way, please stop using ${TORCH_CXX_FLAGS} and ${TORCH_LIBRARIES}, that’s the old CMake way. find_package(Torch ...) provides an imported target torch that you can use directly to do target_link_libraries(AudioPluginExample PRIVATE torch).

1 Like

Is can share a full example of a JUCE plugin that works with Libtorch? I want know how convert torch::Tensor to JUCE and back.
Thanks you

Did you ever figure this out? I am able to build libtorch as a standalone application but trying to run it as a VST fails. Does anyone have example code or a guide for running JUCE with Libtorch in an audio plugin?