Snapcraft can be used to package and distribute C and C++ applications in a way that enables convenient installation by users.
The process of creating a snap for a C or C++ application builds on standard
tools like autotools
and make
, making it possible to adapt
or integrate an application’s existing packaging into the snap building process.
Snaps are defined in a single snapcraft.yaml
file placed in a
snap
folder at the root of your project. This YAML file describes
the application, its dependencies and how it should be built.
The following example shows the entire snapcraft.yaml
file for an
existing project, Moon-buggy:
name: moon-buggy
base: core22
version: '1.0.51'
summary: Drive a car across the moon
description: |
Moon-buggy is a simple character graphics game, where you drive some
kind of car across the moon's surface. Unfortunately there are
dangerous craters there. Fortunately your car can jump over them!
confinement: devmode
parts:
moon-buggy:
plugin: autotools
autotools-configure-parameters: ["--prefix=/usr"]
source: https://github.com/sergiusens/moon-buggy.git
build-packages:
- libncurses-dev
- texinfo
apps:
moon-buggy:
command: usr/bin/moon-buggy
In this example we’re referencing a lightly patched version of moon-buggy, forked from the original moon-buggy source.
This extensive description is broken down in the following sections.
The snapcraft.yaml
file starts with a small amount of human-readable metadata, which usually can be lifted from the GitHub description or project README.md
file. This data is used in the presentation of your app in the Snap Store.
name: moon-buggy
version: '1.0.51'
summary: Drive a car across the moon
description: |
Moon-buggy is a simple character graphics game, where you drive some
kind of car across the moon's surface. Unfortunately there are
dangerous craters there. Fortunately your car can jump over them!
The name
must be unique in the Snap Store. Valid snap names consist of lower-case alphanumeric characters and hyphens. They cannot be all numbers and they also cannot start or end with a hyphen.
By specifying git
for the version, the current git tag or commit will be used as the version string. Versions carry no semantic meaning in snaps.
The summary
can not exceed 79 characters. You can use the pipe symbol ‘|’ in the description
key to declare a multi-line description.
The base keyword declares which base snap to use with the project. A base snap is a special kind of snap that provides a run-time environment alongside a minimal set of libraries that are common to most applications.
base: core22
In this example, core22
is used as the base for snap building, and is based on Ubuntu 22.04 LTS. See Base snaps for more details.
Snaps are containerised to ensure more predictable application behaviour and greater security. Unlike other container systems, the shape of this confinement can be changed through a set of interfaces. These are declarations that tell the system to give permission for specific tasks, such as accessing a webcam or binding to a network port.
The next section describes the level of confinement applied to the running application:
confinement: devmode
It is best to start creating a snap with a confinement level that provides
warnings for confinement issues instead of strictly applying confinement.
This is done by specifying the devmode
(developer mode) confinement value.
When a snap is in devmode, runtime confinement violations will be allowed but reported. These can be reviewed by running journalctl -xe
.
Because devmode is only intended for development, snaps must be set to strict confinement before they can be published as “stable” in the Snap Store. Once an application is working well in devmode, you can review confinement violations, add appropriate interfaces, and switch to strict confinement.
The above example will also work if you change the confinement from devmode
to strict
, as you would before a release.
Parts define what sources are needed to build your application. Parts can be anything: programs, libraries, or other needed assets, but for this example, we only need to use one part: the Moon-buggy git repository.
The plugin
keyword is used to select a language or technology-specific
plugin that knows how to perform the build steps for the project. In this
example, the autotools plugin is used to automate the build, using the standard configure
and make
tools to build the part.
We use the autotools-configure-parameters
option of the autotools
plugin to override the --prefix
, which would default typically default to /usr/local
. While not strictly necessary, it’s useful here to illustrate the use of autotools-configure-parameters
.
parts:
moon-buggy:
plugin: autotools
autotools-configure-parameters: ["--prefix=/usr"]
source: https://github.com/sergiusens/moon-buggy.git
build-packages:
- libncurses-dev
- texinfo
Before building the part, the packages listed in the build-packages
section
need to be installed in the build environment. These are the tools and libraries
that are used during the build process.
For this snap, there is no stage-packages
section specified, because all the moon-buggy
binary doesn’t require any.
It is common for larger, more complex applications to depend on other libraries. The stage-packages
section would be used to list the packages containing those run-time dependencies.
For more details on autotools-specific metadata, see The autotools plugin.
Apps are the commands you want to expose to users, and also the names of any
background services the application provides. Each key under apps
is the command name that should be made available on users’ systems.
The command
specifies the path to the binary to be run. This is resolved relative to the root of the snap contents. In our example, we have overridden the installation path from usr/local/bin
to usr/bin
in the parts
section, above.
apps:
moon-buggy:
command: usr/bin/moon-buggy
If the command name matches the name of the snap specified in the top-level
name
keyword (see the Metadata section above), the binary file will be given
the same name as the snap, as in this example.
If the names differ, the binary file name will be prefixed with the snap name
to avoid naming conflicts between installed snaps. An example of this would be
moon-buggy.some-command
.
If your application is intended to run as a service you simply add the line daemon: simple
after the command keyword. This will automatically keep the service running on install, update, and reboot.
You can download the example repository with the following command:
git clone https://github.com/snapcraft-docs/moon-buggy
After you’ve created the snapcraft.yaml
file (which already exists
in the above repository), you can build the snap by simply executing the snapcraft command in the project directory:
$ snapcraft
Using 'snapcraft.yaml': Project assets will be searched for from the 'snap' directory.
Launching a VM.
[...]
Snapped moon-buggy_1.0.51_amd64.snap
The resulting snap can be installed locally. This requires the --dangerous
flag because the snap is not signed by the Snap Store. The --devmode
flag acknowledges that you are installing an unconfined application:
sudo snap install moon-buggy_*.snap --devmode --dangerous
You can then try it out:
moon-buggy
(screenshots)
Removing the snap is simple too:
sudo snap remove moon-buggy
You can clean up the build environment with the following command:
snapcraft clean
By default, when you make a change to snapcraft.yaml, snapcraft only builds the parts that have changed. Cleaning a build, however, forces your snap to be rebuilt in a clean environment and will take longer.
To share your snaps you need to publish them in the Snap Store. First, create an account on the dashboard. Here you can customise how your snaps are presented, review your uploads and control publishing.
You’ll need to choose a unique “developer namespace” as part of the account creation process. This name will be visible by users and associated with your published snaps.
Make sure the snapcraft
command is authenticated using the email address attached to your Snap Store account:
snapcraft login
You can publish your own version of a snap, provided you do so under a name you have rights to. You can register a name on dashboard.snapcraft.io, or by running the following command:
snapcraft register mysnap
Be sure to update the name:
in your snapcraft.yaml
to match this registered name, then run snapcraft
again.
Use snapcraft to push the snap to the Snap Store.
snapcraft upload --release=edge mysnap_*.snap
If you’re happy with the result, you can commit the snapcraft.yaml to your GitHub repo. You can optionally enable Build from GitHub so any further commits automatically get released to edge, without requiring you to manually build locally.
Congratulations! You’ve just built and published your first C/C++ snap. For a more in-depth overview of the snap building process, see Creating a snap.
Last updated a month ago.