[英]Configured CMake to compile in C++20 but the executable compiles in C++17
我正在嘗試配置 CMake 以使用 C++20 標准編譯我的 C++ 項目,但它一直在 C++17 中編譯。 我在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)
使用 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
以下是我的代碼中需要 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.
}
編譯並運行我的程序后,我為__cplusplus
獲得的輸出是201709
。 誰能指出我在設置CMakeLists.txt
可能做錯了什么?
附加輸出:
$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
$ cmake --version
cmake version 3.21.3
編輯:我剛剛將我的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
所以我在升級到 GCC 版本 11.1.0 后設法使用 C++20 進行編譯。 謝謝@NicolBolas 和@Frank 的見解:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.