Setup gtkmm projects with cmake

Before following along, you will need each of the following utilities. Some folks prefer to use clang++ like I do, however, Ubuntu 16.04 comes with gcc v5.4, which isn't a bad option.

On Ubuntu 16.04, simply run:

sudo apt-get install libgtkmm-3.0-dev  

Source files

In your project folder, create a main.cpp file with the following contents for testing:

#include <gtkmm.h>

int main(int argc, char *argv[]) {

  // #1 create app
  auto app = Gtk::
      Application::
      create(argc, argv, "com.animatedlew.gtkmm");

  // #2 setup main window
  Gtk::Window window;
  window.set_default_size(320, 200);

  // #3 run the app with that window
  return app->run(window);
}

Structure

Create a build directory on the root of your project with mkdir build.

CMakeLists.txt

Create a file on the root of your project called CMakeLists.txt.

First, insert the bare necessities:

cmake_minimum_required(VERSION 2.8.12)  
project(main)  
set(CMAKE_CXX_STANDARD 14)  

Then we want to use pkg-config to retrieve all includes/libs paths necessary to compile gtkmm correctly.

# https://cmake.org/Wiki/CMake:How_To_Find_Libraries
# use pkg-config to find libs and cflags
find_package(PkgConfig)  
pkg_check_modules(GTKMM gtkmm-3.0)  

Grab the environment variables that were set and pass them to cmake.

# pkg-config gtkmm-3.0 --libs
link_directories(${GTKMM_LIBRARY_DIRS})  
# pkg-config gtkmm-3.0 --cflags
include_directories(${GTKMM_INCLUDE_DIRS})  

Finally, grab all your source files and be sure to link the gtkmm libraries.

set (SRC main.cpp)  
add_executable(gui ${SRC})  
target_link_libraries(gui ${GTKMM_LIBRARIES})  

Add the files that need to be compiled to SRC by listing the files after main.cpp, delimited by a space.

Creating CMake Cache

Change into the build directory and run cmake ../. This will create all the files needed to compile your project using make.

Running make

Run make within the build folder.

Run the executable

Since we named our executable gui in our CMakeLists.txt file, we can run our executable by typing in this:

./build/gui

At this point you should see a floating Gtk+ window.

Final thoughts

Now is a good time to flesh out your app. To build the next iteration, simply run make from that build folder.

In the next session, I'll show you how to streamline all this in Atom. You'll have nice things like a linter, auto-completion, switch-to-header, and F9 for building.