How to enable C++14 with CLang in Snapcraft?

Hello! So I have some application called myapp (for example), which needs clang of version 14 or higher for building, and following important part of snapcraft.yaml:

base: core20
confinement: devmode

parts:
    myapp:
        plugin: make
        source-type: local
        source: ./myapp/
        build-packages:
            - clang
            - git
            - python
            - make
            - cmake
            - ninja-build
        override-build: |
            export CC=clang-14
            export CXX=clang++-14
            mkdir build
            cd build
            cmake ..
            ninja

But when I try to build it with:

sudo snapcraft --use-lxd

It returns me on the override-build stage:

CMake Error at /usr/share/cmake-3.16/Modules/CMakeDetermineCCompiler.cmake:49 (message):
  Could not find compiler set in environment variable CC:

  clang-14.
Call Stack (most recent call first):
  CMakeLists.txt:29 (project)


CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage

What am I doing wrong? Maybe there is another way to set 14 version in snap? When I try do it all myself locally without snap (with export CC and CXX commands), everything builds correctly.

You have installed clang from the 20.04 archive using

        build-packages:
            - clang

That will give you “clang version 10.0.0-4ubuntu11”

If you need specifically clang-14, then you’ll need to use an archive (probably a PPA) with it in.

Otherwise, focal-updates would give up to clang12…

$ apt search clang | grep "^clang-[0-9]*/focal"

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

clang-10/focal,now 1:10.0.0-4ubuntu1 amd64 [installed,automatic]
clang-11/focal-updates 1:11.0.0-2~ubuntu20.04.1 amd64
clang-12/focal-updates,focal-security 1:12.0.0-3ubuntu1~20.04.5 amd64
clang-7/focal 1:7.0.1-12 amd64
clang-8/focal 1:8.0.1-9 amd64
clang-9/focal 1:9.0.1-12 amd64

Hmm, I asked my question badly. It’s true that -clang-12 will be newer than just -clang. But that doesn’t seem to be my problem. For example, If I delete export commands and leave snapcraft.yaml as:

base: core20
confinement: devmode

parts:
    myapp:
        plugin: make
        source-type: local
        source: ./myapp/
        build-packages:
            - clang-12
            - git
            - python
            - make
            - cmake
            - ninja-build
        override-build: |
            mkdir build
            cd build
            cmake ..
            ninja

It will return:

CMake Error at cmake/tools.cmake:17 (message):
  GCC version must be at least 11.  For example, if GCC 11 is available under
  gcc-11, g++-11 names, do the following: export CC=gcc-11 CXX=g++-11; rm -rf
  CMakeCache.txt CMakeFiles; and re run cmake or ./release.
Call Stack (most recent call first):
  CMakeLists.txt:46 (include)

If I understand it right (maybe I’m not right), the problem is not in the version of clang, but in the version of C++, which I need somehow to enable to be C++11 or C++14 or higher. And I don’t know how to do exactly that in Snapcraft. I tried to do it using export, but then I got error as in the first message.

Your second error was that you were not installing gcc nor g++ and not setting CC (and CXX). So cmake couldn’t find a compiler (IIRC it tries gcc by default).

Your first error was that you were setting CC (and CXX) to the non-existent clang-14. So cmake couldn’t find the compiler you specified.

If you only need “c++11 or c++14”, then the simplest solution would be to install g++. Then cmake will find it. Or you could use clang, but set CC=clang and CXX=clang++.

If you specifically want to use clang-12, then you need to set CC and CXX accordingly.

Thanks, It helped! Also adding

    sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"

before exports solves the problem.