Compile juce modules to static libraries

This is possible, but not necessarily recommended. In particular, if you choose to build JUCE as a staticlib, you should ensure that all your targets only link your custom “juce static lib” target, and don’t try to pull in extra modules (that is, the staticlib target should be the only target linking JUCE modules). JUCE modules export a JUCE_MODULE_AVAILABLE_modulename=1 preprocessor definition - in order to work properly, it’s important that all modules see the same set of AVAILABLE flags.

To actually build the staticlib, something like this should work:

add_library(my_plugin_modules STATIC)

target_link_libraries(my_plugin_modules
    PRIVATE
        juce::juce_audio_utils
        juce::juce_dsp
        # If you're using your own JUCE-style modules,
        # you should link those here too
    PUBLIC
        juce::juce_recommended_config_flags
        juce::juce_recommended_lto_flags
        juce::juce_recommended_warning_flags)

# We're linking the modules privately, but we need to export
# their compile flags
target_compile_definitions(my_plugin_modules
    PUBLIC
        JUCE_WEB_BROWSER=0
        JUCE_USE_CURL=0
        JucePlugin_Build_Standalone=1
        JUCE_STANDALONE_APPLICATION=JucePlugin_Build_Standalone
    INTERFACE
        $<TARGET_PROPERTY:my_plugin_modules,COMPILE_DEFINITIONS>)

# We also need to export the include directories for the modules
target_include_directories(my_plugin_modules
    INTERFACE
        $<TARGET_PROPERTY:my_plugin_modules,INCLUDE_DIRECTORIES>)

Then, in all code which depends on JUCE or a JUCE module, link my_plugin_modules instead of linking directly to any JUCE module.

1 Like