Setting Up The Project

Create a new git repository in a safe location, this will be where we write the code for our app

Setting up the Meson project

Creating the meson.build file

The meson.build file specifies how our project is configured and compiled.
Create a meson.build file and copy the following to it:

project('example_gtk_application', 'cpp', version: 'v1.0.0', default_options: ['cpp_std=c++17'], meson_version: '>=1.1')

# Define dependencies we want
gtk_dep = dependency('gtk+-2.0')

# If you need the sysroot path, you can get it like this. This is defined
# in the meson-crosscompile.txt file inside the SDK directory
sysroot = meson.get_external_property('sys_root')

###
# Project definition
###
sources = files(
    './src/main.cpp'
)

include_dirs = include_directories(
  './src/include/'
)

executable('example_gtk_application', sources, include_directories: include_dirs, dependencies: [gtk_dep], cpp_args: '-static-libstdc++', link_args: '-static-libstdc++')

Note how we statically link the C++ standard library! Koxtoolchain ships with a GCC version newer than the Kindle technically supports, but this is fine so long as we statically link the standard library.

Your overall file structure should now look something like this:

.
├── meson.build
└── meson.options

1 directory, 2 files

Creating our source files

Now create a file called main.cpp and a folder called include under the src/ folder, your overall file structure should look something like:

.
├── meson.build
├── meson.options
└── src
    ├── include
    └── main.cpp

3 directories, 3 files

Now in main.cpp, copy the following code:

#include <gtk-2.0/gtk/gtk.h>

int main(int argc, char* argv[]) {
  GtkWidget *window;
    
  gtk_init (&argc, &argv);
  
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "L:A_N:application_ID:org.kindlemodding.example-gtk-application_PC:T");

  g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show(window);
  
  gtk_main();

  return 0;
}

Testing compilation

Now we will use meson to make sure we can actually compile our code for both our computer and our target

Configure the Meson project for your computer as so:

mkdir builddir
meson setup builddir

Now we can try to compile our program

meson compile -C builddir

builddir is any arbitrary directory name. You can call it whatever you want, and you can have multiple build directories and switch meson commands between them.

If you run the program on your computer, you should see an empty GTK window:

Testing Cross-Compilation

Now we can compile a version that can run on the Kindle itself:

meson setup --cross-file <meson_crosscompile_path> builddir

Where your target is the same as before and <meson_crosscompile_path> is substituted from the path the SDK installer gave you earlier.

Assuming you had extracted/set up the SDK under the directory ~/kindle-sdks/, this would then look like:

# For HF targets
meson setup --cross-file ~/kindle-sdks/x-tools/arm-kindlehf-linux-gnueabihf/meson-crosscompile.txt builddir
# For SF targets
meson setup --cross-file ~/kindle-sdks/x-tools/arm-kindlepw2-linux-gnueabi/meson-crosscompile.txt builddir

Once it is finished setting up, you can compile it just as you did before:

meson compile -C builddir

If all the above steps work, then you have successfully compiled your first application for the Kindle.

Next step