CMake add_library and JUCE

Hello there,

I am new to JUCE and I’d like to create a library, that uses some Juce classes. What is the best way to do this, as there is no juce_add_library CMake function ? How can I combine add_library and Juce ?

Thanks

You can just use the CMake function add_library().

If you want your library to use JUCE modules, you’ll need to set up your project to use JUCE in a particular way. This post explains how.

@rory indeed, but when I do so, I get compile errors and undefined references to JUCE classes and functions. The same errors occur if I replace juce_add_console_app() by the CMake function add_executable(). Some setup is missing.

@brothernorth Thanks, I’ll give it a try ! I actually started reading this topic but not entirely. As it is dated from 2020, I was wondering if the info in it is outdated or not. This solution seems a bit tricky for a simple thing anyway. Is this really recommended if you can do without a static library ?

Thanks to both of you guys !

The latest advice in that thread is up-to-date; if you check the reply timestamps you’ll see I revived the thread about a month ago to get some more detail on it (probably should have started a new thread, naughty me…)

I would say it’s not so tricky to set up if you’re a bit used to CMake. It’s certainly not the ‘recommended’ way of doing things, but it depends what you want to do as to whether it’s worth the effort. For my use-case, I was building an app that converted audio files into SysEx data and then flashed it to a drum machine; I wanted to provide both a command-line app and a GUI app, so it made sense to split the audio processing logic into a separate reusable target. I also wanted to write unit tests (using google test) to ensure the audio processing was correct. So for my case it made sense.

As to your compile errors, note that

  1. you can’t do #include <JuceHeader.h> any more; you have to include the individual headers directly, so make sure you’ve added them correctly (both as a link target in your JUCE dependencies target, and via the includes in your library target);

  2. you might need to add the juce:: namespace explicitly when you address any JUCE classes in your code. Read through the post I linked to previously in its entirety, more details are in there.

1 Like