How to include Juce Modules (headers only) in static library (Projucer) without including .cpp?

I have a static library being managed by the Projucer. This question applies specifically to Windows and VS 2019.

I need to include several juce module’s headers, but it seems that when I put the modules in the Projucer file, I get all the juce source files compiled into the library, because of these .cpp includes that are generated by the Projucer:

Screenshot 2021-07-01 092943 (2)

When I then compile the static lib into the main Juce Project (which includes all the same modules), I get hundreds of “multiply defined symbols” errors.

4>include_juce_audio_plugin_client_VST3.cpp
3>plughostDB.lib(include_juce_gui_basics.obj) : error LNK2005: "private: static class juce::Desktop * juce::Desktop::instance" (?instance@Desktop@juce@@0PEAV12@EA) already defined in KCORE_mo_win_d.lib(include_juce_gui_basics.obj)
3>plughostDB.lib(include_juce_gui_basics.obj) : error LNK2005: "int juce::numActiveScopedDpiAwarenessDisablers" (?numActiveScopedDpiAwarenessDisablers@juce@@3HA) already defined in KCORE_mo_win_d.lib(include_juce_gui_basics.obj)
3>plughostDB.lib(include_juce_gui_basics.obj) : error LNK2005: "public: static class juce::ModifierKeys juce::HWNDComponentPeer::modifiersAtLastCallback" (?modifiersAtLastCallback@HWNDComponentPeer@juce@@2VModifierKeys@2@A) already defined in KCORE_mo_win_d.lib(include_juce_gui_basics.obj)
3>plughostDB.lib(include_juce_gui_basics.obj) : error LNK2005: "public: static char const * const juce::Toolbar::toolbarDragDescriptor" (?toolbarDragDescriptor@Toolbar@juce@@2QEBDEB) already defined in KCORE_mo_win_d.lib(include_juce_gui_basics.obj)
3>plughostDB.lib(include_juce_gui_basics.obj) : error LNK2005: "public: static class juce::ModifierKeys juce::ModifierKeys::currentModifiers" (?currentModifiers@ModifierKeys@juce@@2V12@A) already defined in KCORE_mo_win_d.lib(include_juce_gui_basics.obj)
etc...

If I manually delete these "include_juce_XXX_.cpp" files from the lib, it builds fine, and then it compiles into the main project with no problems. But I don’t want to have to manually delete these all the time.

There are no actual juce functions called in the library; I just need some definitions from the headers.

Is there a way to set the Projucer to include the correct modules in JuceHeader.h and AppConfig.h for the library, without including all the source code? In other words, I just want to see the AppConfig.h and JuceHeaders.h files in the static lib.

My best advice would be to modify the staticlib to build as a JUCE module instead. That way, it can include and depend-on the JUCE modules that it requires, but each module will only be built into your final project once.

I’ve worked on a project structured in this way in the past, and it was a very painful experience. Eventually I converted the staticlib part to a module and the build became much easier to manage.

2 Likes

Thanks. Will think about that, although I wonder how difficult it would be to change at this point…