JUCE + torch application crashes at starting time [Visual Studio]

Hello everyone :slight_smile:
I’m trying to build a JUCE plugin with an AI component written using torch. I am able to compile an application depending on torch only, and a basic JUCE plugin without torch dependency with no problems. However, when I try to combine them I get this runtime error:
Exception thrown at 0x00007FFAAAC7F90D (ntdll.dll) in minimal_plugin.exe: 0xC0000139: Entry Point Not Found.
Do you have any idea why it is triggering this error here?
I’ll attach my CMakeLists.txt file aswell:

cmake_minimum_required ( VERSION 3.15)
project ( minimal_plugin VERSION 0.0.1)

# TORCH compatibility

set ( CMAKE_PREFIX_PATH "../libs/libtorch")
find_package ( Torch REQUIRED )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} ")



add_subdirectory (C:/Users/rizzo/JUCE ./JUCE )

juce_add_plugin (minimal_plugin
    COMPANY_NAME Daevide
    PLUGIN_MANUFACTURER_CODE D43
    PLUGIN_CODE Abc1
    FORMATS AU VST3 Standalone
    PRODUCT_NAME "minimal_plugin"
)

juce_generate_juce_header ( minimal_plugin )

target_sources ( minimal_plugin
    PRIVATE
    Source/PluginEditor.cpp
    Source/PluginEditor.h
    Source/PluginProcessor.cpp
    Source/PluginProcessor.h)

target_compile_definitions(minimal_plugin
    PUBLIC
        # JUCE_WEB_BROWSER and JUCE_USE_CURL would be on by default, but you might not need them.
        JUCE_WEB_BROWSER=0  # If you remove this, add `NEEDS_WEB_BROWSER TRUE` to the `juce_add_plugin` call
        JUCE_USE_CURL=0     # If you remove this, add `NEEDS_CURL TRUE` to the `juce_add_plugin` call
        JUCE_VST3_CAN_REPLACE_VST2=0)

target_link_libraries(minimal_plugin
    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)

set_property ( TARGET minimal_plugin PROPERTY CXX_STANDARD 17)

if (MSVC)
  file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
  add_custom_command(TARGET minimal_plugin
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $<TARGET_FILE_DIR:minimal_plugin>)
endif (MSVC)

Thank you! :slight_smile:

You’re missing some pieces:

cmake_minimum_required(VERSION 3.15)

project(JUCE_LibTorch_Plugin VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# JUCE Setup
set(JUCE_DIR "${CMAKE_SOURCE_DIR}/juce")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${JUCE_DIR}/extras/Build/CMake")
find_package(JUCE CONFIG REQUIRED)

# LibTorch Setup
set(Torch_DIR "${CMAKE_SOURCE_DIR}/libs/libtorch")
find_package(Torch REQUIRED)

# JUCE Plugin configuration
juce_add_plugin(JUCE_LibTorchPlugin
    COMPANY_NAME "YourCompany"
    PLUGIN_MANUFACTURER_CODE Juce
    PLUGIN_CODE Ltch
    FORMATS VST3 AU Standalone
    PRODUCT_NAME "JUCE_LibTorchPlugin"
)

# Source files
target_sources(JUCE_LibTorchPlugin
    PRIVATE
        Source/PluginProcessor.cpp
        Source/PluginProcessor.h
        Source/PluginEditor.cpp
        Source/PluginEditor.h
)

# Link JUCE and LibTorch
target_link_libraries(JUCE_LibTorchPlugin
    PRIVATE
        juce::juce_audio_utils
        juce::juce_audio_processors
        juce::juce_core
        ${TORCH_LIBRARIES}
)

# Include LibTorch headers
target_include_directories(JUCE_LibTorchPlugin PRIVATE ${TORCH_INCLUDE_DIRS})

# Copy LibTorch shared libraries
add_custom_command(TARGET JUCE_LibTorchPlugin
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${Torch_DIR}/lib ${CMAKE_BINARY_DIR}/lib
)

The problem was that Visual Studio was building using some torch dlls related to a different version to the one specified on CMakeLists.txt. However, i still have to manually copy the dlls in the same folder of the output executable. I guess this part of your code:

# Copy LibTorch shared libraries
add_custom_command(TARGET JUCE_LibTorchPlugin
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${Torch_DIR}/lib ${CMAKE_BINARY_DIR}/lib
)

will fix it.
Thanks and see you!