简体   繁体   English

将 CMake 配置为在 C++20 中编译,但可执行文件在 C++17 中编译

[英]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 .编辑:我刚刚将我的gccg++版本升级到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.

相关问题 operator==() 代码编译 w/C++17 但不编译 C++20 - operator==() code compiles w/C++17 but not C++20 如果无法编译未使用的函数,则模板类可以使用 C++17 编译,但不能使用 C++20 编译 - Template class compiles with C++17 but not with C++20 if unused function can't be compiled 在 c++17 中编译的简单代码在 c++20 中产生错误 - simple code that compile in c++17 produce error with c++20 c++17 (c++20) 无序 map 和自定义分配器 - c++17 (c++20) unordered map and custom allocator 在现代 C++11 / C++14 / C++17 和未来的 C++20 中枚举到字符串 - enum to string in modern C++11 / C++14 / C++17 and future C++20 C++17/20 - 使用<filesystem>确定文件是否可执行</filesystem> - C++17/20 - Use <filesystem> to determine if file is executable 为什么`std :: reference_wrapper`在c ++ 17中被弃用,而在c ++ 20中被删除? - Why is `std::reference_wrapper` deprecated in c++17 and removed in c++20? 为什么 rebind <U>::other 在 C++17 中被弃用并在 C++20 中被删除?</u> - Why rebind<U>::other are deprecated in C++17 and removed in C++20? 在 C++17 和 C++20 之间可移植地使用 UTF-8 字符串文字前缀 - Using UTF-8 string-literal prefixes portably between C++17 and C++20 模板友元 function 中的 C++17 和 C++20 与一元和二元运算符的区别 - Difference of C++17 and C++20 in template friend function with unary and binary operators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM