The cmake plugin

The cmake plugin is useful for building CMake-based parts. This plugin uses the common plugin keywords as well as those for sources. For more information, see Snapcraft parts metadata.

This plugin also supports options from the make plugin.

A cmake project will typically include a CMakeLists.txt file to drive the build, and the plugin requires that CMakeLists.txt exists within the root of the source tree.

Plugin-specific features and syntax are dependent on which base is being used, as outlined below:

For a simple example, see MOOS applications, or search GitHub for projects already using the plugin.

ⓘ This is a snapcraft plugin. See Snapcraft plugins and Supported plugins for further details on how plugins are used.

base: core22

  • cmake-generator (string, default: Unix Makefiles) Determine what native build system is to be used. Can be either Ninja or Unix Makefiles (default).
  • cmake-parameters (list of strings) Configure flags to pass to the build using the common cmake semantics.

Note that Snapcraft does not specify cmake parameters by default. A common parameter for parts using the cmake plugin is CMAKE_INSTALL_PREFIX=/usr - this prevents installation to the default directory of /usr/local. For example:

parts:
  hello:
    plugin: cmake
    cmake-parameters:
      - -DCMAKE_INSTALL_PREFIX=/usr
    ....

Requires Snapcraft version 7.0+.

base: core20

  • cmake-generator (string, default: Unix Makefiles) Determine what native build system is to be used. Can be either Ninja or Unix Makefiles (default).
  • cmake-parameters (list of strings) Configure flags to pass to the build using the common cmake semantics.

Note that Snapcraft does not specify cmake parameters by default. A common parameter for parts using the cmake plugin is CMAKE_INSTALL_PREFIX=/usr - this prevents installation to the default directory of /usr/local. For example:

parts:
  hello:
    plugin: cmake
    cmake-parameters:
      - -DCMAKE_INSTALL_PREFIX=/usr
    ....

Requires Snapcraft version 4.0+.

base: core18 | core

  • configflags (list of strings) Configure flags to pass to the build using the common cmake semantics.

Requires Snapcraft version 3.0+.

It may be worth noting that cmake-based snaps can fail to build with a “No CMAKE_CXX_COMPILER could be found” error. The solution is to add g++ to the build packages section of the part, like so:

parts:
mypart:
build-packages: [g++]

It may be that g++ should be automatically included when a cmake plugin is used, but that was not the case for me using snapcraft v3.1.

1 Like

@degville
ninja should be Ninja, i.e. with capital N.
Thanks.

Good spot - thanks for letting us know. It’s fixed now.

This still appears to be the case - that g++ is an unspecified requirement.

@degville perhaps the requirement should be mentioned in https://snapcraft.io/docs/cmake-plugin ?

1 Like

surely g++ is only a requirement for projects that use c++ code. for other projects cmake shouldn’t require it.

1 Like

Very good, thanks, I didn’t know that.

Hi all,

I am trying below yaml, but snapcraft build stuck. Keep going forever.? Any idea?

name: my-snap-name # you probably want to 'snapcraft register <name>'
base: core20 # the base snap is the execution environment for this snap
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
summary: Single-line elevator pitch for your amazing snap # 79 char long summary
description: |
  This is my-snap's description. You have a paragraph or two to tell the
  most important story about your snap. Keep it under 100 words though,
  we live in tweetspace and your description wants to look good in the snap
  store.

grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots

parts:
    cmake-build:
        plugin: cmake
        source: .
        build-packages:
            - gcc

@snahmad75 - Try running snapcraft with --enable-developer-debug and post the output in a new forum post. From there, I can help you debug further.