Saturday, October 30, 2021

[SOLVED] Configured CMake to compile in C++20 but the executable compiles in C++17

Issue

I am trying to configure CMake to compile my C++ project using the C++20 standard, but it keeps compiling in C++17. My compiler settings in CMakeLists.txt are as follows:

cmake_minimum_required(VERSION 3.21.3 FATAL_ERROR)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
option(OPTIMISER "Optimiser level 3" OFF)
set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 20)

I get the following output when setting up my build directory with CMake:

Configuring CMake files...
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python: /usr/bin/python3.9 (found version "3.9.7") found components: Interpreter 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/niran90/Dropbox/Projects/Apeiron/build

The following is a minimal example in my code that requires C++20 features (requires a constexpr std::fill function):

#include <array>
#include <algorithm>

template <class t_data_type, int array_size>
constexpr auto InitializeStaticArray(const t_data_type& _init_value)
{
  std::array<t_data_type, array_size> initialised_array;
  std::fill(initialised_array.begin(), initialised_array.end(), _init_value);
  return initialised_array;
}

int main()
{
  constexpr auto test = InitializeStaticArray<double, 3>(1.0); // This code does not compile.
}

After compiling and running my program, the output that I get for __cplusplus is 201709. Can anyone point out what I might me doing wrong in setting up my CMakeLists.txt?

Additional Outputs:

$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

$ cmake --version 
cmake version 3.21.3

Edits: I have just upgraded my gcc and g++ versions to 10.3.0 and I am still unable to compile with C++20.

$ gcc --version
gcc (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0

$ g++ --version
g++ (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0

Solution

So I have managed to compile using C++20 after upgrading to GCC version 11.1.0. Thank you @NicolBolas and @Frank for the insights :)



Answered By - niran90