Specs:
- WSL, ubuntu 22.04
- cmake version 3.25.2
- g++ version: 11.4.0
- gcc version: 11.4.0
I was doing lab0 of CS144, Spring 2023 of Stanford when I got stuck at the very beginning. When I ran cmake --build build
, I got the following error message:
error: invalid return type ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} of ‘constexpr’ function ‘static constexpr std::string ExpectationViolation::boolstr(bool)’
16 | static constexpr std::string boolstr( bool b ) { return b ? "true" : "false"; }
I found out on the Internet that this was because constexpr std::string
was introduced in C++ 20 and what I had on my wsl was gcc-11 and g++-11, and gcc-11 is only supports some experimental features of C++ 20. So I intend to set the default compiler of Cmake to g++. However, I keep getting the same error message.
i have tried installing g++-12:
lrwxrwxrwx 1 root root 21 Sep 27 21:24 /usr/bin/g++ -> /etc/alternatives/g++*
lrwxrwxrwx 1 root root 23 May 13 15:52 /usr/bin/g++-11 -> x86_64-linux-gnu-g++-11*
lrwxrwxrwx 1 root root 23 May 13 15:33 /usr/bin/g++-12 -> x86_64-linux-gnu-g++-12*
lrwxrwxrwx 1 root root 21 Sep 27 21:46 /usr/bin/gcc -> /etc/alternatives/gcc*
lrwxrwxrwx 1 root root 23 May 13 15:52 /usr/bin/gcc-11 -> x86_64-linux-gnu-gcc-11*
lrwxrwxrwx 1 root root 23 May 13 15:33 /usr/bin/gcc-12 -> x86_64-linux-gnu-gcc-12*
lrwxrwxrwx 1 root root 9 Aug 5 2021 /usr/bin/gcc-ar -> gcc-ar-11*
lrwxrwxrwx 1 root root 26 May 13 15:52 /usr/bin/gcc-ar-11 -> x86_64-linux-gnu-gcc-ar-11*
lrwxrwxrwx 1 root root 26 May 13 15:33 /usr/bin/gcc-ar-12 -> x86_64-linux-gnu-gcc-ar-12*
lrwxrwxrwx 1 root root 9 Aug 5 2021 /usr/bin/gcc-nm -> gcc-nm-11*
lrwxrwxrwx 1 root root 26 May 13 15:52 /usr/bin/gcc-nm-11 -> x86_64-linux-gnu-gcc-nm-11*
lrwxrwxrwx 1 root root 26 May 13 15:33 /usr/bin/gcc-nm-...
It doesn’t work. Then i tried building a soft link:
sudo update-alternatives --config gcc
There is only one alternative in link group gcc (providing /usr/bin/gcc): /usr/bin/g++-11
Nothing to configure.
sudo update-alternatives --config g++
There is only one alternative in link group g++ (providing /usr/bin/g++): /usr/bin/g++-11
Nothing to configure.
It still does not work. I am now clueless.
2
Answers
gcc/g++ is the compiler and 11.4.0 is the compiler’s version not the language’s
c++ 20 is a c++ standard
the two things are different
you need to change the cmake (CMakeLists.txt) to use different c++ standard
something like this:
or alternatively dont use those features
A quick and non-invasive way to make CMake use another C++ compiler is to set the
CXX
environment variable:As the CMake documentation explains, the result is cached so you need to delete
CMakeCache.txt
(or your whole build directory) before the changes apply.Caching may also be the reason why your previous attempts didn’t work out.
BTW, to set a C compiler, use the
CC
environment variable.