1

I have a C++ project and I want to test the compatibility of library headers with different compiler versions. I have a simple source file (that includes said headers) and I want to change the compiler argument to std=gnu++11 for this one target. How do I do that?

executable('old_compiler_test', ['octest.cxx']
    # override ARGS here ??? how
)

Note that I have

add_global_arguments(
    ['-std=gnu++17',
    ....

rather than the dedicated option for this, in spite of the warning to prefer the special option, because the special option simply doesn't work. (Why is a question I've never tracked down)

update

To clarify: I'm not trying to make additional configurations in the same way that debug and release are configurations. I want a different compiler argument to be applied to a single target within the configuration.

JDługosz
  • 5,592
  • 3
  • 24
  • 45

1 Answers1

0

From the Meson documentation, you can use the argument <languagename>_args to pass additional compiler arguments. In your case, since you use C++, it would give something like

executable('old_compiler_test', ['octest.cxx'],
    cpp_args: ['std=gnu++11']
)

However the documentation also specify that there are no way to disable an argument added by add_global_argument(), so you will end up with both -std=gnu++17 and -std=gnu++11 passed to the compiler. I don't know how your compiler will behave, but I tried to pass both arguments to GCC 10.2 and it uses c++17 (not what you want).

Workaround

It seems that if you define the C++ version in the project() statement, Meson will removes it if an other version is specified in compiler arguments, giving the behaviour you expect.

Here is the sample I used:

meson.build

project('project-name', 'cpp',
   default_options: ['cpp_std=c++17']
)

executable('old_compiler_test', ['octest.cxx'],
    cpp_args: ['-std=gnu++11']
)

octest.cxx

#include <iostream>
int main() {
    std::cout << __cplusplus << std::endl;
}

After compilation, running the executable will print 201103, which means that the compiler used c++11 as desired.

vvanpelt
  • 791
  • 1
  • 6
  • 13