Using CMake to build gtkmm programs

Currently, most people use automake and autoconf to define their build structure, but there are alternatives. For instance, this document shows how to use pkg-config within a CMake script.

The example CMakeLists.txt file shown below can be used to build the radio buttons example from the gtkmm package, located in examples/book/buttons/radiobutton/. Just substitute the names of source files and it should work for any gtkmm program. Do the following:

setup your project skeleton

You are not forced a use any reasonable dir structure, but for didactic reasons we will use this one

  • -project_topdir
        -src_dir

throughout this document.

pkg-config support

Autoconf-style pkg-config support exists in cmake. It is now unnecessary to copy it yourself into your project dir.

Write (or copy) the source files

Later you will write your own source files, but for testing we pull the sources of the radio-button example into the src_dir ( the gtkmm-tarball contains them at ./examples/book/buttons/radiobutton/ )

-project_topdir
    -src_dir
        - main.cc 
        - radiobuttons.cc  
        - radiobuttons.h
    -my_cmake_scripts 
        - FindPkgConfig.cmake

write the CMakeLists.txt

We will write even two of them. One play the role of configure.ac in project_topdir, the other replaces Makefile.am in src_dir

-project_topdir
    - CMakeLists.txt
    -src_dir
        - CMakeLists.txt
        - main.cc 
        - radiobuttons.cc  
        - radiobuttons.h
    -my_cmake_scripts 
        - FindPkgConfig.cmake

The CMakeLists.txt in project_topdir:

project(example)

find_package(PkgConfig)

pkg_check_modules(GTKMM gtkmm-2.4) # look into FindPkgConfig.cmake, 
                                                                # it contains documentation
# Now the variables GTKMM_INCLUDE_DIRS, GTKMM_LIBRARY_DIRS and GTKMM_LIBRARIES 
# contain what you expect 

add_subdirectory(src_dir) 

The CMakeLists.txt in src_dir:

link_directories(
    ${GTKMM_LIBRARY_DIRS}  )

include_directories(
    ${GTKMM_INCLUDE_DIRS}  )

add_executable(example # name of the executable on Windows will be example.exe 
    main.cc 
    radiobuttons.cc 
    radiobuttons.h )

target_link_libraries(example 
    ${GTKMM_LIBRARIES}  )

build the binaries

Now we will extend the project tree one more time by adding one dir for the debugging and one for the release binaries.

-project_topdir
    - CMakeLists.txt
    -src_dir
        - CMakeLists.txt
        - main.cc 
        - radiobuttons.cc  
        - radiobuttons.h
    -my_cmake_scripts 
        - FindPkgConfig.cmake
    -build_debug
    -build_release

change to the build_debug dir, set and run...

  • Linux
    $ cmake -DCMAKE_BUILD_TYPE=Debug ../
  • Windows
    $ cmake -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug ../

or use the cmake-gui

  • Linux
    $ ccmake -DCMAKE_BUILD_TYPE=Debug ../
  • Windows
    $ CMakeSetup -DCMAKE_BUILD_TYPE=Debug ../
    click configure and choose i.e. MinGW when you're asked for the build-system

A bunch of Makefiles will be created by cmake. Now run make

  • Linux
    $ make
  • Windows
    $ mingw32-make

Do the same at the build_release dir but with -DCMAKE_BUILD_TYPE=Release

run the binaries

  • Linux
    $ ./build_debug/src_dir/example
    $ ./build_release/src_dir/example
  • Windows
    $ build_debug\src_dir\example.exe
    $ build_release\src_dir\example.exe

further reading


Projects/gtkmm/UsingCMake (last edited 2013-11-23 00:07:01 by WilliamJonMcCann)