How to use includes correctly (incomplete types)

Hi there :smiley:

I have trouble when I try to include some JUCE headers in my own project. For example, if I want to use MidiMessageSequence class, I have to include <juce_audio_basics/midi/juce_MidiMessage.h> before <juce_audio_basics/midi/juce_MidiMessageSequence.h>, otherwise I get a compile error saying that MidiMessage does not name a type. And I have to include <juce_core/juce_core.h> before or I get incomplete type error.

And indeed, when I open the corresponding header files, there is no #include in them. How to fix this strange behavior ? This seems like JUCE is configured to use precompiled headers.

Here is a very simple project reproducing the issue :

Files
CMakeLists.txt
project(ConsoleAppTemplate VERSION 0.1)

set (TargetName ${PROJECT_NAME})

juce_add_console_app(${TargetName} PRODUCT_NAME "Console App Example")

target_sources(${TargetName} PRIVATE Source/Main.cpp)

target_compile_definitions(${TargetName} PRIVATE
        JUCE_WEB_BROWSER=0
        JUCE_USE_CURL=0)

target_link_libraries(${TargetName} PRIVATE
        juce::juce_recommended_config_flags
        juce::juce_recommended_lto_flags
        juce::juce_recommended_warning_flags
        juce::juce_core
        juce::juce_audio_basics)
Source
Main.cpp
#include <juce_core/juce_core.h>  // <-- Needed
#include <juce_audio_basics/midi/juce_MidiMessage.h>  // <-- Needed too
#include <juce_audio_basics/midi/juce_MidiMessageSequence.h>

int main()
{
    auto test = juce::MidiMessageSequence();
    return 0;
}

Just include „JuceHeader.h“ that’s being generated by Projucer. If you want to be more cautious with includes, I think you can go with only including specific module headers, but you have to get their dependencies right.

To include a single module (ie not using juceHeader.h), you should #include juce_audio_badics.h

1 Like

Hello,

@Rincewind, I do not use the Projucer for my project but cmake :wink: Okay then, this behavior is not from a bad cmake configuration when building JUCE :slight_smile:

@benvining If I include the whole juce_audio_basics.h only for one class, it will include a lot of unused code. In my humble opinion it is better to include only what you use. I mean the JUCE audio basics module is still pretty big, even if it is only one module. But I’ll go for this solution if it’s the only way to do things cleanly.

Thanks to both of you guys !
Cheers

You’re absolutely right about that, but JUCE does not intend to support the use case of including individual headers. You would have to manually sort out the right order of includes yourself (and it may change in the future, too).

1 Like