Meson picks the wrong compiler (GCC instead on clang)
Asked Answered
P

1

7

I am trying to configure my project to build with LLVM/clang++, however GCC is always picked:

$ /opt/llvm/clang+llvm-7.0.1-x86_64-linux-gnu-ubuntu-18.04/bin/clang++ --version
clang version 7.0.1 (tags/RELEASE_701/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/llvm/clang+llvm-7.0.1-x86_64-linux-gnu-ubuntu-18.04/bin
$ 
$ CC=/opt/llvm/clang+llvm-7.0.1-x86_64-linux-gnu-ubuntu-18.04/bin/clang++ meson ~/build
The Meson build system
Version: 0.49.1
Source dir: ~/source
Build dir: ~/build
Build type: native build
Project name: test
Project version: 0.1.0
Native C++ compiler: ccache c++ (gcc 6.3.0 "c++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516")
Build machine cpu family: x86_64
Build machine cpu: x86_64
Message: Compiler: GCC
Dependency threads found: YES 
Build targets in project: 2
Found ninja-1.8.2 at /opt/ninja

The operating systems is Debian Linux 9.

This is the meson.build file:

## meson.build

project('MyPrj', 'cpp',
        version: '0.1.0',
        meson_version: '>= 0.44',
        license: 'GPL',
        default_options: [
            'cpp_std=c++17',
            'warning_level=3',
            'buildtype=release'
        ]
       )

#
# Compiler configuration
# To set a default compiler, from the OS shell:
#   $ CC=mycc meson <options>

compiler = meson.get_compiler('cpp')

warning_level = get_option('warning_level').to_int()

if compiler.get_id() == 'gcc'
    message('Compiler: GCC')
    libs_compiler = ['-lstdc++fs']
    libs_linker   = ['-lstdc++fs']
elif compiler.get_id() == 'clang'
    message('Compiler: LLVM/clang')
    libs_compiler = ['-stdlib=libc++']
    libs_linker   = ['-stdlib=libc++', '-lstdc++fs']
endif

add_global_arguments('-Wall', '-Wextra', '-Wmissing-declarations',
                     '-Wno-unused', # keep this commented out
                     libs_compiler,
                     language: 'cpp')

add_global_link_arguments(libs_linker, language: 'cpp')

# '.' will refer to current build directory
include_dirs = include_directories('.')

src = [ 'test.cc' ]

# External dependencies

thread_dep = dependency('threads')

# Build

test_a = static_library('test', src,
                           include_directories : include_dirs,
                          )

test_exe = executable('testexe', src,
                           include_directories : include_dirs
                          )

How can I convince Meson to use clang?

Phosphorylase answered 13/2, 2019 at 18:50 Comment(3)
Did you set up the env variables? i.e. CC=clang and CXX=clang++? Then run maybe doing meson build-clang. That should probably work.Hercule
@Hercule - Ops, I specified the C++ compiler with the CC environment variable, instead of CXX. Now it works. If you copy your comment as an answer I will accept it.Phosphorylase
Done, glad it workedHercule
H
8

Set up the relevant C/C++ environmental variables. i.e. CC=clang and CXX=clang++. Then run the build using meson build-clang.

That will solve the issue.

Hercule answered 13/2, 2019 at 23:1 Comment(3)
I erroneously thought cc was for C, and CC for C++.Phosphorylase
It makes no sense at all, that you cannot specify it in meson.build file.Sepalous
By the way, setting CC and/or CXX disables ccache: mesonbuild.com/Feature-autodetection.html#ccacheParing

© 2022 - 2024 — McMap. All rights reserved.