JuceHeader and user modules with Cmake?

How to properly add custom modules (switching from Projucer to CMake)?

I have a folder /Libraries (sibling to JUCE) with my modules (and following CMakeLists.txt).

juce_add_modules(ALIAS_NAMESPACE spaghettis
spaghettis_core
spaghettis_engine
spaghettis_gui)

I use CMakeLists.txt above.

cmake_minimum_required(VERSION 3.26)

project(Spaghettis VERSION 0.9)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN TRUE)

set(JUCE_ENABLE_MODULE_SOURCE_GROUPS TRUE CACHE BOOL “” FORCE)

add_subdirectory(JUCE)
add_subdirectory(Libraries)

file(READ “${CMAKE_CURRENT_SOURCE_DIR}/Resources/plist.txt” PLIST_CONTENT)

juce_add_gui_app(Spaghettis
COMPANY_NAME					“Spaghettis”
COMPANY_COPYRIGHT				“Copyright (c) 1997 Miller Puckette and others.”
ICON_BIG        				“${CMAKE_CURRENT_SOURCE_DIR}/Resources/Icons/PNG/spaghettis_icon.png”
PRODUCT_NAME    				“Spaghettis”
MICROPHONE_PERMISSION_ENABLED	TRUE
MICROPHONE_PERMISSION_TEXT		“Spaghettis requires access to the Microphone.”
HARDENED_RUNTIME_ENABLED		TRUE
HARDENED_RUNTIME_OPTIONS		com.apple.security.device.audio-input
com.apple.security.cs.debugger
com.apple.security.cs.disable-library-validation
PLIST_TO_MERGE 					“${PLIST_CONTENT}”
BUNDLE_ID       				org.puredata.spaghettis)

juce_generate_juce_header(Spaghettis)

target_sources(Spaghettis PRIVATE Source/Main.cpp)

target_compile_definitions(Spaghettis
PRIVATE
JUCE_STRICT_REFCOUNTEDPOINTER=1
JUCE_WEB_BROWSER=0
JUCE_USE_CURL=0)

set_target_properties(Spaghettis PROPERTIES ENABLE_EXPORTS TRUE)

target_link_libraries(Spaghettis
PRIVATE
juce::juce_core
juce::juce_data_structures
juce::juce_events
juce::juce_graphics
juce::juce_gui_basics
juce::juce_gui_extra
spaghettis::spaghettis_core
spaghettis::spaghettis_engine
spaghettis::spaghettis_gui
PUBLIC
juce::juce_recommended_config_flags
juce::juce_recommended_lto_flags
juce::juce_recommended_warning_flags)

But it fails to compile. Apparently an include path is missing somewhere


nicolasdanet@Air-de-Nicolas Spaghettis % cmake --build build
[ 6%] Generating Spaghettis_artefacts/JuceLibraryCode/Spaghettis/PkgInfo
[ 12%] Generating Spaghettis_artefacts/JuceLibraryCode/JuceHeader.h
[ 18%] Building CXX object CMakeFiles/Spaghettis.dir/Source/Main.cpp.o
In file included from /Users/nicolasdanet/Work/S/Spaghettis/Source/Main.cpp:9:
In file included from /Users/nicolasdanet/Work/S/Spaghettis/Source/Main.hpp:9:
/Users/nicolasdanet/Work/S/Spaghettis/build/Spaghettis_artefacts/JuceLibraryCode/JuceHeader.h:20:10: fatal error: ‘spaghettis_core/spaghettis_core.h’ file not found
#include <spaghettis_core/spaghettis_core.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/Spaghettis.dir/Source/Main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Spaghettis.dir/all] Error 2
make: *** [all] Error 2

I have no clue what i did wrong, and what should i do. An idea?

The problem is that in the Projucer if your module header file use an “.hpp" extension (e.g. “spaghettis_core.hpp") it is properly handled in the auto-generated “JuceHeader.h" ; whereas juce_generate_juce_header function do NOT (it badly includes unexisting “spaghettis_core/spaghettis_core.h").

[SOLVED] Thus always use ".h” extension for your JUCE modules! :laughing:

1 Like

That sounds like a JUCE bug.

And another reason not to use the JuceHeader.

1 Like

Is there an easy way to avoid JuceHeader.h whereas keeping the Projucer stuff working?

Refactor your C++ code to always include the actual module headers you need.

The process is basically, for every source file, remove #include <JuceHeader.h>. Then for each unknown symbol, include the module header of the module it lives in.

When you’re done, remove the generation of the juce header from both your cmake & PJ project files.

2 Likes

Thanks ; i’ll probably try.

99% of my sources are modules ; don’t now if it will make the process easier or worse. :thinking:

Code inside JUCE modules especially should not #include <JuceHeader.h>, because now your module only works in juce projects that generate a JUCE header!

1 Like

Of course, i include JuceHeader.h once (in Main file).

But how do you disable JuceHeader.h generation inside/from Projucer?