The Build System
Compiling Binaries & Installing Data with Meson
The next thing we need is a build system. The build system that we're going to be using is called Meson. We already installed the
mesonprogram at the beginning of this book when we installed
elementary-sdk. What we're going to do in this step is create the files that tell Meson how to install your program. This includes all the rules for building your source code as well as correctly installing your icons, .desktop, and metainfo files and the binary app that results from the build process.
Create a new file in your project's root folder called "meson.build". We've included some comments along the way to explain what each section does. You don't have to copy those, but type the rest into that file:
# project name and programming language
project('com.github.yourusername.yourrepositoryname', 'vala', 'c')
# Create a new executable, list the files we want to compile, list the dependencies we need, and install
'src' / 'Application.vala',
# Install our .desktop file so the Applications Menu will see it
'data' / 'hello-again.desktop',
install_dir: get_option('datadir') / 'applications',
rename: meson.project_name() + '.desktop'
# Install our .metainfo.xml file so AppCenter will see it
'data' / 'hello-again.metainfo.xml',
install_dir: get_option('datadir') / 'metainfo',
rename: meson.project_name() + '.metainfo.xml'
Notice that in each of our
install_datamethods, we rename our files using our project name. By using our project name—and its RDNN scheme—we will ensure that our files are installed under a unique name that won't cause conflicts with other apps.
And you're done! Your app now has a real build system. This is a major milestone in your app's development!
Now that we have a build system, let's try it out. Configure the build directory using the Meson command in Terminal:
meson build --prefix=/usr
This command tells Meson to get ready to build our app using the prefix "/usr" and that we want to build our app in a clean directory called "build". The
mesoncommand defaults to installing our app locally, but we want to install our app for all users on the computer.
Change into the build directory and use
ninjato build. Then, if the build is successful, install with
If all went well, you should now be able to open your app from the Applications Menu and pin it to the Dock. We'll revisit Meson again later to add some more complicated behavior, but for now this is all you need to know to give your app a proper build system. If you want to explore Meson a little more on your own, you can always check out Meson's documentation.
Let's review all we've learned to do:
- Create a new Gtk app using
- Keep our projects organized into branches
- License our app under the GPL and declare our app's authors in a standardized manner
- Create a .desktop file using RDNN that tells the computer how to display our app in the Applications Menu and the Dock
- Set up a Meson build system that contains all the rules for building our app and installing it cleanly
That's a lot! You're well on your way to becoming a bona fide app developer for elementary OS. Give yourself a pat on the back, then take some time to play around with this example. Change the names of files and see if you can still build and install them properly. Ask another developer to clone your repo from GitHub and see if it builds and installs cleanly on their computer. If so, you've just distributed your first app! When you're ready, we'll move onto the next section: Translations.