Specifying gcc compiler version to use

15 views
Skip to first unread message

Dlugosz, John M

unread,
Jun 22, 2023, 1:15:15 PM6/22/23
to The Meson Build System
We're running RHEL on AWS, and have installed gcc 12, so it now appears in /opt/rh/gcc-toolset-12/root/usr/bin/gcc .  If we set the PATH and LD_LIBRARY_PATH for the users in the etc/profile file, it works on any old Bash prompt, e.g. typing >gcc --version will give me 12.2.1.  But from that _same_ prompt, running >meson build.debug will show that it's finding gcc version 8.5.0, and I see pathnames inside the generated build.ninja like /usr/lib/gcc/x86_64-redhat-linux/8/libstdc .

This is maddening, and is blocking all development work.

How do I tell meson to use the desired installed compiler version?

Eli Schwartz

unread,
Jun 22, 2023, 2:56:43 PM6/22/23
to The Meson Build System
On 6/22/23 1:15 PM, 'Dlugosz, John M' via The Meson Build System wrote:
> We're running RHEL on AWS, and have installed gcc 12, so it now appears
> in /opt/rh/gcc-toolset-12/root/usr/bin/gcc . If we set the PATH and
> LD_LIBRARY_PATH for the users in the etc/profile file, it works on any old
> Bash prompt, e.g. typing >gcc --version will give me 12.2.1. But from that
> _same_ prompt, running >meson build.debug will show that it's finding gcc
> version 8.5.0, and I see pathnames inside the generated build.ninja
> like /usr/lib/gcc/x86_64-redhat-linux/8/libstdc

There's a subtle oddity when it comes to having multiple compilers in
different locations on $PATH. It probably helps to get an overview of
how meson internally detects a compiler. So here it is:

- first, check if meson's machine file toolchain config files are
defined in this build: https://mesonbuild.com/Machine-files.html
and if so, use whatever binaries are specified there

- next, check for the industry standard $CC variable, and use it if set

- if neither a machine file "c" binary nor $CC is set, check for
compilers by probing for some common names:
- cc
- gcc
- clang
- nvc
- pgcc
- icc
- icx


So keeping that in mind -- what happens when the first "gcc" in $PATH is
in /opt, but there is also a "cc" in /usr/bin ? Meson finds "cc" and
uses it -- it is probably a symlink to /usr/bin/gcc though.

The build log should hint to what happened -- you'll see a line that
looks something like this:

```
C compiler for the host machine: ccache cc (gcc 8.5.0 "cc (GCC) 11.1.0")
```

The part in the parentheses is the compiler ID and version string. The
part outside of the parentheses is the executable name that will be run.

In my case, ccache was automatically detected as available, and "cc" was
the first compiler found -- so the generated build.ninja will contain
compiler rules that run e.g.


```
rule c_COMPILER
command = ccache cc $ARGS -MD -MQ $out -MF $DEPFILE -o $out -c $in
deps = gcc
depfile = $DEPFILE_UNQUOTED
description = Compiling C object $out
```

or

```
ccache cc -MD -MQ progname.p/foo.o -MF progname.p/foo.o.d -o
progname.p/foo.o -c ../foo.c
```


> How do I tell meson to use the desired installed compiler version?


You should be able to force meson to use whichever gcc is first, by
setting `CC=gcc` as an environment variable and then setting $PATH to
have gcc-toolset-12 come at the beginning. Alternatively, you could set
CC=/opt/rh/gcc-toolset-12/root/usr/bin/gcc of course. :)

(You may want to set other programs from the same location, e.g. $AR --
this will always detect the right one if $PATH is set, because there's
only one name to lookup, "ar", not seven of them.)


--
Eli Schwartz

Dlugosz, John M

unread,
Jun 22, 2023, 3:38:09 PM6/22/23
to Frediano Ziglio, The Meson Build System
We tried that again, carefully.  Exported both CC and CXX, with the full path to gcc.
Meson complains :
Compiler /opt/rh/gcc-toolset-12/root/usr/bin/gcc can not compile programs.
The log file has more details:
Sanity testing C++ compiler: /opt/rh/gcc-toolset-12/root/usr/bin/gcc
Is cross compiler: False.
Sanity check compiler command line: /opt/rh/gcc-toolset-12/root/usr/bin/gcc sanitycheckcpp.cc -o sanitycheckcpp.exe -D_FILE_OFFSET_BITS=64
Sanity check compile stdout:-----
Sanity check compile stderr:
gcc: fatal error: cannot execute ‘cc1plus’: execvp: No such file or directory
compilation terminated.-----meson.build:1:0: ERROR: Compiler /opt/rh/gcc-toolset-12/root/usr/bin/gcc can not compile programs.



On Thu, Jun 22, 2023 at 1:28 PM Frediano Ziglio <fred...@gmail.com> wrote:
Il giorno gio 22 giu 2023 alle ore 18:15 'Dlugosz, John M' via The
Meson Build System <meson...@googlegroups.com> ha scritto:

>
> We're running RHEL on AWS, and have installed gcc 12, so it now appears in /opt/rh/gcc-toolset-12/root/usr/bin/gcc .  If we set the PATH and LD_LIBRARY_PATH for the users in the etc/profile file, it works on any old Bash prompt, e.g. typing >gcc --version will give me 12.2.1.  But from that _same_ prompt, running >meson build.debug will show that it's finding gcc version 8.5.0, and I see pathnames inside the generated build.ninja like /usr/lib/gcc/x86_64-redhat-linux/8/libstdc .
>
> This is maddening, and is blocking all development work.
>
> How do I tell meson to use the desired installed compiler version?
>

Hi,
  did you try setting the CC environment?
Did you try removing meson build directory and setting it up again?

Frediano

Dlugosz, John M

unread,
Jun 22, 2023, 3:44:59 PM6/22/23
to Eli Schwartz, The Meson Build System
I get the idea that there might be some .ini file laying around that gives the compiler to use.  But I'm lost in trying to find what it's using.  Was there a meson command that lists all the current settings (including this file's location)?


On Thu, Jun 22, 2023 at 1:57 PM Eli Schwartz <eschw...@gmail.com> wrote:
On 6/22/23 1:15 PM, 'Dlugosz, John M' via The Meson Build System wrote:
> We're running RHEL on AWS, and have installed gcc 12, so it now appears
> in /opt/rh/gcc-toolset-12/root/usr/bin/gcc .  If we set the PATH and
> LD_LIBRARY_PATH for the users in the etc/profile file, it works on any old
> Bash prompt, e.g. typing >gcc --version will give me 12.2.1.  But from that
> _same_ prompt, running >meson build.debug will show that it's finding gcc
> version 8.5.0, and I see pathnames inside the generated build.ninja
> like /usr/lib/gcc/x86_64-redhat-linux/8/libstdc

There's a subtle oddity when it comes to having multiple compilers in
different locations on $PATH. It probably helps to get an overview of
how meson internally detects a compiler. So here it is:

- first, check if meson's machine file toolchain config files are

Jacob Alexander

unread,
Jun 22, 2023, 3:49:27 PM6/22/23
to The Meson Build System
I haven't tried this in a while, but I had a lot of trouble in the past with certain options in meson.

I detail in the bug, but I had to use a cross file or wrapper script to work around the problem.
This was quite a while ago so there may be some more options now.

Eli Schwartz

unread,
Jun 22, 2023, 3:53:52 PM6/22/23
to Dlugosz, John M, Frediano Ziglio, The Meson Build System
On 6/22/23 3:37 PM, 'Dlugosz, John M' via The Meson Build System wrote:
> We tried that again, carefully. Exported both CC and CXX, with the full
> path to gcc.
> Meson complains :
>
> Compiler /opt/rh/gcc-toolset-12/root/usr/bin/gcc can not compile programs.
>
> The log file has more details:
>
> Sanity testing C++ compiler: /opt/rh/gcc-toolset-12/root/usr/bin/gcc
> Is cross compiler: False.
> Sanity check compiler command line:
> /opt/rh/gcc-toolset-12/root/usr/bin/gcc sanitycheckcpp.cc -o
> sanitycheckcpp.exe -D_FILE_OFFSET_BITS=64
> Sanity check compile stdout:-----
> Sanity check compile stderr:
> gcc: fatal error: cannot execute ‘cc1plus’: execvp: No such file or directory
> compilation terminated.-----meson.build:1:0: ERROR: Compiler
> /opt/rh/gcc-toolset-12/root/usr/bin/gcc can not compile programs.


This seems quite off... are you able to execute the compiler yourself
(outside of meson) to compile test executables? Having it fail with
"cannot execute ‘cc1plus’: execvp: No such file or directory" implies
that the compiler itself is very broken and non-functional.


--
Eli Schwartz

Eli Schwartz

unread,
Jun 22, 2023, 3:57:13 PM6/22/23
to Dlugosz, John M, The Meson Build System
On 6/22/23 3:44 PM, Dlugosz, John M wrote:
> I get the idea that there might be some .ini file laying around that gives
> the compiler to use. But I'm lost in trying to find what it's using. Was
> there a meson command that lists all the current settings (including this
> file's location)?


https://mesonbuild.com/Native-environments.html

It wouldn't be laying around -- meson will only use a .ini file to give
the compiler to use, *iff* that file is specified on the command line
with either the --native-file= or the --cross-file= argument.



--
Eli Schwartz

Dlugosz, John M

unread,
Jun 22, 2023, 4:00:37 PM6/22/23
to Eli Schwartz, Eliza Gail Maxwell, Frediano Ziglio, The Meson Build System
You are right.  `gcc --version` works and gives version 12, but we just tried compiling a Hello World program directly on the Bash prompt, and it gave the same error about `cc1plus`.  Thanks for the insight.


Reply all
Reply to author
Forward
0 new messages