You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Qt 5.15.16使用C++20而非C++17?

问题:Qt编译始终默认使用C17,无法切换到C20

尝试在Qt中启用C20标准,但无论如何配置,编译环境始终默认使用C17。以下是相关配置信息:

配置输出(config.summary)

# head config.summary
Build type: linux-g++ (x86_64, CPU features: mmx sse sse2)
Compiler: gcc 13.1.0
Configuration: sse2 aesni sse3 ssse3 sse4_1 sse4_2 avx avx2 avx512f
 avx512bw avx512cd avx512dq avx512er avx512ifma avx512pf avx512vbmi
 avx512vl compile_examples enable_new_dtags f16c largefile precompile_header
 rdrnd rdseed shani x86SimdAlways shared shared rpath release 
c++11 c++14 c++17 c++1z concurrent dbus reduce_exports reduce_relocations stl

Build options:
  Using C standard ....................... C11
  Using C++ standard ..................... C++17

Dockerfile内容

FROM ubuntu:18.04

# Install gcc-13 PPA
RUN echo "deb [trusted=yes] http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic main" > /etc/apt/sources.list.d/ubuntu-toolchain-r-test.list && \
    apt update && \
    apt install -y --no-install-recommends gcc-13 g++-13 && \
    update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 100 && \
    update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 100

WORKDIR /opt/build

RUN sed -i 's|# deb|deb|' /etc/apt/sources.list && \
    apt update && \
    apt install -y --no-install-recommends git libedit-dev libopenal-data libsndio-dev libopenal1 libopenal-dev pulseaudio libpthread-workqueue-dev && \
    apt build-dep -y qtbase-opensource-src && \
    apt-get purge -y gcc-7 g++-7 cpp-7 gcc-7-base && \
    update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 100 && \
    update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 100 && \
    apt install -y wget xz-utils && \
    wget -P /opt/ https://cdimage.debian.org/mirror/qt.io/qtproject/archive/qt/5.15/5.15.16/single/qt-everywhere-opensource-src-5.15.16.tar.xz && \
    tar -xf /opt/qt-everywhere-opensource-src-5.15.16.tar.xz -C /opt/

当前构建命令

docker run -it --rm IAMGENAME bash
export CXXFLAGS="-std=c++20"
../qt-everywhere-src-5.15.16/configure -bundled-xcb-xinput -xcb -xcb-xlib -pulseaudio -alsa -opensource -confirm-license  -recheck-all -v -nomake tests -nomake examples

解决方案

Qt 5.15.x系列默认最高仅支持到C17,单独设置CXXFLAGS无法覆盖configure脚本的默认逻辑,必须通过显式配置参数启用C20:

1. 修改configure命令

直接在configure中添加-std=c++20参数,同时可结合-cxxflags确保编译器参数生效:

# 方式一:先导出环境变量再配置
export CXXFLAGS="-std=c++20"
../qt-everywhere-src-5.15.16/configure -std=c++20 -bundled-xcb-xinput -xcb -xcb-xlib -pulseaudio -alsa -opensource -confirm-license -recheck-all -v -nomake tests -nomake examples

# 方式二:直接在configure中指定cxxflags
../qt-everywhere-src-5.15.16/configure -cxxflags="-std=c++20" -std=c++20 -bundled-xcb-xinput -xcb -xcb-xlib -pulseaudio -alsa -opensource -confirm-license -recheck-all -v -nomake tests -nomake examples

2. 清理旧配置缓存

如果之前的配置缓存导致参数不生效,添加-clean参数清理后重新配置:

../qt-everywhere-src-5.15.16/configure -clean -std=c++20 [其他原有参数]

3. 版本兼容性说明

Qt 5.15对C++20的支持是实验性的,部分模块可能存在兼容性问题;同时确保gcc 13.1.0已正确设置为默认编译器,可通过gcc --version和g++ --version验证。

4. 验证结果

重新配置后,查看config.summary中的Using C++ standard字段,确认已切换为C++20。

内容的提问来源于stack exchange,提问作者Andrey Pupkin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:47:06