CMake for dynamic linked library AND lib, minor issue

I’ve successfully followed some advice for using CMake to make a JUCE dynamic linked library.

Now I want to use JUCE functions in another project by linking against a JUCE lib. I think it’s necessary to set JUCE_DLL_BUILD=1 to export the functions and get a lib file too.

So I tried adding this line to CMakeLists.txt

add_compile_definitions(JUCE_DLL_BUILD=1)

However, now when building the solution I get a single error:

Error C2247 'juce::Thread::operator delete' not accessible because 'juce::dsp::BackgroundMessageQueue' uses 'private' to inherit from 'juce::Thread'

Any advice on how to proceed? Thank you.

Seems foolish but if I change that line to
class BackgroundMessageQueue : public Thread
I can get a .lib

Thanks for raising, this should be fixed on develop now:

I’m not sure exactly what approach you ended going with to build a DLL, but the cleanest approach is probably something like this:

add_library(my_juce_dll SHARED)

target_link_libraries(my_juce_dll
    PRIVATE
        juce::juce_audio_utils
        juce::juce_dsp
    PUBLIC
        juce::juce_recommended_config_flags
        juce::juce_recommended_lto_flags
        juce::juce_recommended_warning_flags)

target_compile_definitions(my_juce_dll
    PUBLIC
        # Required flags for DLL build
        JUCE_DLL_BUILD=1
        JUCE_STANDALONE_APPLICATION=1
        # Other feature flags
        JUCE_WEB_BROWSER=0 
        JUCE_USE_CURL=0
    INTERFACE
        $<TARGET_PROPERTY:my_juce_dll,COMPILE_DEFINITIONS>)

target_include_directories(my_juce_dll
    INTERFACE
        $<TARGET_PROPERTY:my_juce_dll,INCLUDE_DIRECTORIES>)

set_target_properties(my_juce_dll PROPERTIES
    POSITION_INDEPENDENT_CODE TRUE
    VISIBILITY_INLINES_HIDDEN TRUE
    C_VISIBILITY_PRESET hidden
    CXX_VISIBILITY_PRESET hidden)

This will produce a target named my_juce_dll which properly exports the necessary preprocessor definitions and include directories, along with the recommended JUCE flags.

1 Like