[英]Configured CMake to compile in C++20 but the executable compiles in C++17
I am trying to configure CMake to compile my C++ project using the C++20 standard, but it keeps compiling in C++17.我正在尝试配置 CMake 以使用 C++20 标准编译我的 C++ 项目,但它一直在 C++17 中编译。 My compiler settings in
CMakeLists.txt
are as follows:我在
CMakeLists.txt
中的编译器设置如下:
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:使用 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):以下是我的代码中需要 C++20 功能的最小示例(需要
constexpr
std::fill
函数):
#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
.编译并运行我的程序后,我为
__cplusplus
获得的输出是201709
。 Can anyone point out what I might me doing wrong in setting up my CMakeLists.txt
?谁能指出我在设置
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
和g++
版本升级到10.3.0
,但我仍然无法使用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
So I have managed to compile using C++20 after upgrading to GCC version 11.1.0.所以我在升级到 GCC 版本 11.1.0 后设法使用 C++20 进行编译。 Thank you @NicolBolas and @Frank for the insights :)
谢谢@NicolBolas 和@Frank 的见解:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.