How I use CMake with JUCE

In another thread, someone was asking me about how I use CMake. I decided to start this thread to describe how I use CMake in Linux to build my JUCE project.

First thing I do is use Introjucer to get the JUCE components I want. In my case, it is a GUI application, no audio nor opengl. I let Introjucer copy all the relevant JUCE source files to [color=#0000FF]MyProject/JuceLibraryCode/…[/color]

My own source files are broken up into multiple source directories to help me organize functionality. E.g.:
[list]
[][color=#0000FF]MyProject/src-foo[/color][/]
[][color=#0000FF]MyProject/src-bar[/color][/]
[][color=#0000FF]MyProject/src-blah[/color][/]
[][color=#0000FF]MyProject/src-juce[/color][/][/list]

Note that last entry. I created the directory [color=#0000FF]MyProject/src-juce[/color] but all it contains is a single [color=#0000FF]CMakeLists.txt[/color] file without any actual source files. More on this below.

The relevant part of the project’s top-level CMake file contains this:

INCLUDE_DIRECTORIES ( BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/JuceLibraryCode )
ADD_SUBDIRECTORY ( src-juce )
ADD_SUBDIRECTORY ( src-foo )
ADD_SUBDIRECTORY ( src-bar  )
...etc...

Nothing surprising there, it just causes CMake to look for more CMake files located in those subdirectories.

Here is the entire contents of [color=#0000FF]MyProject/src-juce/CMakeLists.txt[/color]. This is the one that builds JUCE as a static library I can link against:

FIND_LIBRARY    ( RT           rt          )
FIND_LIBRARY    ( DL           dl          )
FIND_PACKAGE    ( X11          REQUIRED    )
FIND_PACKAGE    ( Threads      REQUIRED    )
FIND_PACKAGE    ( Freetype     REQUIRED    )
INCLUDE_DIRECTORIES ( AFTER ${FREETYPE_INCLUDE_DIRS} )
SET ( JUCE_LIBRARIES ${RT} ${DL} ${X11_LIBRARIES} ${FREETYPE_LIBRARIES} )
SET ( JUCE_SOURCE
        ${CMAKE_HOME_DIRECTORY}/JuceLibraryCode/modules/juce_core/juce_core.cpp
        ${CMAKE_HOME_DIRECTORY}/JuceLibraryCode/modules/juce_data_structures/juce_data_structures.cpp
        ${CMAKE_HOME_DIRECTORY}/JuceLibraryCode/modules/juce_events/juce_events.cpp
        ${CMAKE_HOME_DIRECTORY}/JuceLibraryCode/modules/juce_graphics/juce_graphics.cpp
        ${CMAKE_HOME_DIRECTORY}/JuceLibraryCode/modules/juce_gui_basics/juce_gui_basics.cpp
        ${CMAKE_HOME_DIRECTORY}/JuceLibraryCode/modules/juce_gui_extra/juce_gui_extra.cpp
    )
ADD_LIBRARY ( myproject_juce STATIC ${JUCE_SOURCE} )
TARGET_LINK_LIBRARIES ( myproject_juce ${CMAKE_THREAD_LIBS_INIT} ${JUCE_LIBRARIES} )

That gives me a static library I can reference within CMake called [color=#0000FF]myproject_juce[/color] which contains everything I need from JUCE.

The rest of my CMakeLists.txt files are relatively simple. For example, [color=#0000FF]MyProject/blah/CmakeLists.txt[/color] would contain:

FILE ( GLOB_RECURSE BLAH_SOURCE *.cpp )
LIST ( SORT BLAH_SOURCE )
INCLUDE_DIRECTORIES ( BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/JucerDialogs/ )
ADD_EXECUTABLE ( blah ${BLAH_SOURCE} )
TARGET_LINK_LIBRARIES ( blah ${CMAKE_THREAD_LIBS_INIT} myproject_foo myproject_bar myproject_juce )

So at the time I link the [color=#0000FF]blah[/color] application, I tell it to link against [color=#0000FF]myproject_juce[/color], and everything comes together nicely.

Note on line 3 how I have a subdirectory [color=#0000FF]MyProject/src-blah/JucerDialogs[/color] with a few dialog windows I originally built using The Jucer. If I was to do it over or take the time to fix things up, I’d move those into a different [color=#0000FF]MyProject/src-dialogs[/color] directory to keep things a bit cleaner.

Hey stephan thanks for sharing,

I have some doubts to ask, SO if i create static library from window dll project provided with Juce code's extra,

What do i need to do ? is following fine ? It would be great if you can share cmake files, which I can refer.

I have cmake for my project, but that does not contain Juce for a now, and i want to add juce to that porject.

add_library(Juce.lib)
include_directories ("${PROJECT_SOURCE_DIR}/JuceLibraryCode")

add_subdirectory (Modules)  # And other sub directories also required ?

# add the executable add_executable (Tutorial tutorial.cxx)

target_link_libraries (Tutorial juce)

Thanks for your help.