I have a projucer project that builds a static library and uses juceheader.h
I’m trying to migrate it to cmake now. In cmake there is no juce specific command for building a static library, so I can’t use juce_generate_juce_header() call in my CMakeLists.txt file. I read in documentation that this is optional for cmake projects and you can directly include what you use.
However, my problem is that my project also relies on ProjectInfo data, defined in JuceHeader.h
Is there a solution/nice workaround for this scenario?
sorry, I’m not sure I follow. If I add juce_generate_juce_header call in my CMakeLists, I get the following error
CMake Error at build/debug/vcpkg_installed/arm64-osx/share/juce/JUCEUtils.cmake:514 (message):
Target foo_core does not have a generated sources directory. Ensure it
was created with a juce_add_* function
Call Stack (most recent call first):
backend/core/CMakeLists.txt:27 (juce_generate_juce_header)
my library is generated with add_library() since juce doesn’t have any specific cmake command for that.
Ok, I’ve tested it and it works quite good. I’ve created my own JuceHeader.h.in
With this content
/*
* NOTE: This file would be auto-generated by CMake, to make up
* for the lack of auto generation of JuceHeader.h by the juce cmake
* implementation for targets that are not produced by calling
* juce_add_*() call
*/
#pragma once
#include <juce_core/juce_core.h>
#include <juce_events/juce_events.h>
#if ! JUCE_DONT_DECLARE_PROJECTINFO
namespace ProjectInfo
{
const char* const projectName = "@PROJECT_NAME@";
const char* const companyName = "MyCompany";
const char* const versionString = "@CMAKE_PROJECT_VERSION@";
const int versionNumber = 0x0;
}
#endif
And in my CMakeLists.txt I’ve added the following lines:
# this is needed to generate "fake" JuceHeader.h file
# the content will be the same as the one projucer generates
# but we will have control over some variables directly from CMake
configure_file(
${CMAKE_SOURCE_DIR}/backend/core/JuceHeader.h.in
${CMAKE_SOURCE_DIR}/backend/core/Include/JuceHeader.h
)
# add our JuceHeader.h to the include directories
target_include_directories(${PROJECT_NAME} SYSTEM
PRIVATE
${CMAKE_SOURCE_DIR}/backend/core/Include
)
What I like about it is that you don’t need to change anything in your source files, they can continue #include <JuceHeader.h> and access stuff from ProjectInfo namespace
Hope it will be helpful for someone.
Note, there are couple of gotchas here, like if you add more modules in your project, you will need to manually keep “your” JuceHeader.h in sync with the real one (if you like me and need to support both Projucer and Cmake side by side).