将 CMake 配置为在 C++20 中编译,但可执行文件在 C++17 中编译
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 的见解:)
我正在尝试配置 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 的见解:)