【问题标题】:Configured CMake to compile in C++20 but the executable compiles in C++17将 CMake 配置为在 C++20 中编译,但可执行文件在 C++17 中编译
【发布时间】:2021-12-06 13:44:57
【问题描述】:

我正在尝试配置 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 功能的最小示例(需要 constexprstd::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

修改: 我刚刚将我的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

【问题讨论】:

  • 您的代码是否使用任何 C++20 功能?如果是这样,它会编译吗?您的编译器是否通过该宏宣传 C++20 支持?
  • @NicolBolas 我希望我的代码使用 C++20 功能,并且我在帖子中添加了一个示例。但是,它不会编译。你能详细说明你的最后一个问题吗?是否有不同的特定于编译器的宏来从编译器调用 C++20 支持?
  • 我指的是不完全支持 C++20 的编译器可能不会将 __cplusplus 重新定义为 C++20 的值。更改该值可能会被解释为表示完全支持 C++20 的所有功能,而实现可能还不支持这些功能。您是否检查过 constexpr std::fill 是否被宣传为您的标准库支持的 C++20 功能?
  • 您正在使用 gcc 9.3 进行编译,它仅部分支持 c++20:gcc.godbolt.org/z/PjvE8K8s6
  • @NicolBolas 和@Frank 我刚刚将我的gccg++ 版本升级到10.3.0。不幸的是,我仍然遇到同样的问题。

标签: c++ cmake compilation c++17 c++20


【解决方案1】:

所以我在升级到 GCC 版本 11.1.0 后设法使用 C++20 进行编译。谢谢@NicolBolas 和@Frank 的见解:)

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 2020-08-31
    • 2020-10-17
    • 2015-03-27
    • 2012-08-06
    • 2012-10-02
    • 2022-07-06
    • 2012-12-03
    相关资源
    最近更新 更多