环境准备

系统版本

$ cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

安装编译环境

$ sudo yum install gcc gcc-c++ glibc-static wget xz bzip2

$ gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

下载源码

$ wget https://ftp.gnu.org/gnu/gcc/gcc-10.2.0/gcc-10.2.0.tar.xz

编译

$ tar -xvf gcc-10.2.0.tar.xz
$ cd gcc-10.2.0
$ ./contrib/download_prerequisites
$ mkdir build
$ cd build

$ sudo mkdir /usr/lib/gcc/x86_64-redhat-linux/10.2.0
$ ../configure --prefix=/usr/lib/gcc/x86_64-redhat-linux/10.2.0  --enable-bootstrap  --enable-checking=release --enable-languages=c,c++ --disable-multilib
$ make

... 数小时的等待

$ sudo make install

配置

$ sudo mv /usr/bin/gcc /usr/bin/gcc-4.8.5
$ sudo mv /usr/bin/g++ /usr/bin/g++-4.8.5
$ sudo alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8.5 88 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8.5
$ sudo alternatives --install /usr/bin/gcc gcc /usr/lib/gcc/x86_64-redhat-linux/10.2.0/bin/x86_64-pc-linux-gnu-gcc 99 --slave /usr/bin/g++ g++ /usr/lib/gcc/x86_64-redhat-linux/10.2.0/bin/x86_64-pc-linux-gnu-g++
$ sudo alternatives --config gcc
$ sudo rm -f /usr/lib64/libstdc++.so.6
$ sudo ln -s /usr/lib/gcc/x86_64-redhat-linux/10.2.0/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so.6

$ strings /usr/lib64/libstdc++.so.6 | grep GLIBC

验证

$ gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-redhat-linux/10.2.0/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper
目标:x86_64-pc-linux-gnu
配置为:../configure --prefix=/usr/lib/gcc/x86_64-redhat-linux/10.2.0 --enable-bootstrap --enable-checking=release --enable-languages=c,c++ --disable-multilib
线程模型:posix
Supported LTO compression algorithms: zlib
gcc 版本 10.2.0 (GCC) 

$ g++ -v
使用内建 specs。
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-redhat-linux/10.2.0/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper
目标:x86_64-pc-linux-gnu
配置为:../configure --prefix=/usr/lib/gcc/x86_64-redhat-linux/10.2.0 --enable-bootstrap --enable-checking=release --enable-languages=c,c++ --disable-multilib
线程模型:posix
Supported LTO compression algorithms: zlib
gcc 版本 10.2.0 (GCC)

使用cpp20特征测试代码测试一下

$ g++ test.cpp
$ ./a.out

C++ GENERAL
__cplusplus                                  201402
__cpp_exceptions                             199711
__cpp_rtti                                   199711

C++11 CORE
__cpp_alias_templates                        200704
__cpp_attributes                             200809
__cpp_constexpr                              201304
__cpp_decltype                               200707
__cpp_delegating_constructors                200604
__cpp_inheriting_constructors                201511
__cpp_initializer_lists                      200806
__cpp_lambdas                                200907
__cpp_nsdmi                                  200809
__cpp_range_based_for                        200907
__cpp_raw_strings                            200710
__cpp_ref_qualifiers                         200710
__cpp_rvalue_references                      200610
__cpp_static_assert                          200410
__cpp_threadsafe_static_init                 200806
__cpp_unicode_characters                     200704
__cpp_unicode_literals                       200710
__cpp_user_defined_literals                  200809
__cpp_variadic_templates                     200704

C++14 CORE
__cpp_aggregate_nsdmi                        201304
__cpp_binary_literals                        201304
__cpp_constexpr                              201304
__cpp_decltype_auto                          201304
__cpp_generic_lambdas                        201304
__cpp_init_captures                          201304
__cpp_return_type_deduction                  201304
__cpp_sized_deallocation                     201309
__cpp_variable_templates                     201304

C++14 LIB
__cpp_lib_chrono_udls                        201304
__cpp_lib_complex_udls                       201309
__cpp_lib_exchange_function                  201304
__cpp_lib_generic_associative_lookup         201304
__cpp_lib_integer_sequence                   201304
__cpp_lib_integral_constant_callable         201304
__cpp_lib_is_final                           201402
__cpp_lib_is_null_pointer                    201309
__cpp_lib_make_reverse_iterator              201402
__cpp_lib_make_unique                        201304
__cpp_lib_null_iterators                     201304
__cpp_lib_quoted_string_io                   201304
__cpp_lib_result_of_sfinae                   201210
__cpp_lib_robust_nonmodifying_seq_ops        201304
__cpp_lib_shared_timed_mutex                 201402
__cpp_lib_string_udls                        201304
__cpp_lib_transformation_trait_aliases       201304
__cpp_lib_transparent_operators              201510
__cpp_lib_tuple_element_t                    201402
__cpp_lib_tuples_by_type                     201304

C++17 CORE
__cpp_aggregate_bases                        ------
__cpp_aligned_new                            ------
__cpp_capture_star_this                      ------
__cpp_constexpr                              201304
__cpp_deduction_guides                       ------
__cpp_enumerator_attributes                  ------
__cpp_fold_expressions                       ------
__cpp_guaranteed_copy_elision                ------
__cpp_hex_float                              201603
__cpp_if_constexpr                           ------
__cpp_inheriting_constructors                201511
__cpp_inline_variables                       ------
__cpp_namespace_attributes                   ------
__cpp_noexcept_function_type                 ------
__cpp_nontype_template_args                  ------
__cpp_nontype_template_parameter_auto        ------
__cpp_range_based_for                        200907
__cpp_static_assert                          200410
__cpp_structured_bindings                    ------
__cpp_template_template_args                 ------
__cpp_variadic_using                         ------

C++17 LIB
__cpp_lib_addressof_constexpr                ------
__cpp_lib_allocator_traits_is_always_equal   201411
__cpp_lib_any                                ------
__cpp_lib_apply                              ------
__cpp_lib_array_constexpr                    ------
__cpp_lib_as_const                           ------
__cpp_lib_atomic_is_always_lock_free         ------
__cpp_lib_bool_constant                      ------
__cpp_lib_boyer_moore_searcher               ------
__cpp_lib_byte                               ------
__cpp_lib_chrono                             ------
__cpp_lib_clamp                              ------
__cpp_lib_enable_shared_from_this            201603
__cpp_lib_execution                          ------
__cpp_lib_filesystem                         ------
__cpp_lib_gcd_lcm                            ------
__cpp_lib_hardware_interference_size         ------
__cpp_lib_has_unique_object_representations  ------
__cpp_lib_hypot                              ------
__cpp_lib_incomplete_container_elements      201505
__cpp_lib_invoke                             ------
__cpp_lib_is_aggregate                       ------
__cpp_lib_is_invocable                       ------
__cpp_lib_is_swappable                       201603
__cpp_lib_launder                            ------
__cpp_lib_logical_traits                     ------
__cpp_lib_make_from_tuple                    ------
__cpp_lib_map_try_emplace                    ------
__cpp_lib_math_special_functions             ------
__cpp_lib_memory_resource                    ------
__cpp_lib_node_extract                       ------
__cpp_lib_nonmember_container_access         ------
__cpp_lib_not_fn                             ------
__cpp_lib_optional                           ------
__cpp_lib_parallel_algorithm                 ------
__cpp_lib_raw_memory_algorithms              ------
__cpp_lib_sample                             ------
__cpp_lib_scoped_lock                        ------
__cpp_lib_shared_mutex                       ------
__cpp_lib_shared_ptr_arrays                  201611
__cpp_lib_shared_ptr_weak_type               ------
__cpp_lib_string_view                        ------
__cpp_lib_to_chars                           ------
__cpp_lib_transparent_operators              201510
__cpp_lib_type_trait_variable_templates      ------
__cpp_lib_uncaught_exceptions                201411
__cpp_lib_unordered_map_try_emplace          ------
__cpp_lib_variant                            ------
__cpp_lib_void_t                             201411

C++20 CORE
__cpp_aggregate_paren_init                   ------
__cpp_char8_t                                ------
__cpp_concepts                               ------
__cpp_conditional_explicit                   ------
__cpp_consteval                              ------
__cpp_constexpr                              201304
__cpp_constexpr_dynamic_alloc                ------
__cpp_constexpr_in_decltype                  ------
__cpp_constinit                              ------
__cpp_deduction_guides                       ------
__cpp_designated_initializers                ------
__cpp_generic_lambdas                        201304
__cpp_impl_coroutine                         ------
__cpp_impl_destroying_delete                 ------
__cpp_impl_three_way_comparison              ------
__cpp_init_captures                          201304
__cpp_modules                                ------
__cpp_nontype_template_args                  ------
__cpp_using_enum                             ------

C++20 LIB
__cpp_lib_array_constexpr                    ------
__cpp_lib_assume_aligned                     ------
__cpp_lib_atomic_flag_test                   ------
__cpp_lib_atomic_float                       ------
__cpp_lib_atomic_lock_free_type_aliases      ------
__cpp_lib_atomic_ref                         ------
__cpp_lib_atomic_shared_ptr                  ------
__cpp_lib_atomic_value_initialization        ------
__cpp_lib_atomic_wait                        ------
__cpp_lib_barrier                            ------
__cpp_lib_bind_front                         ------
__cpp_lib_bit_cast                           ------
__cpp_lib_bitops                             ------
__cpp_lib_bounded_array_traits               ------
__cpp_lib_char8_t                            ------
__cpp_lib_chrono                             ------
__cpp_lib_concepts                           ------
__cpp_lib_constexpr_algorithms               ------
__cpp_lib_constexpr_complex                  ------
__cpp_lib_constexpr_dynamic_alloc            ------
__cpp_lib_constexpr_functional               ------
__cpp_lib_constexpr_iterator                 ------
__cpp_lib_constexpr_memory                   ------
__cpp_lib_constexpr_numeric                  ------
__cpp_lib_constexpr_string                   ------
__cpp_lib_constexpr_string_view              ------
__cpp_lib_constexpr_tuple                    ------
__cpp_lib_constexpr_utility                  ------
__cpp_lib_constexpr_vector                   ------
__cpp_lib_coroutine                          ------
__cpp_lib_destroying_delete                  ------
__cpp_lib_endian                             ------
__cpp_lib_erase_if                           ------
__cpp_lib_execution                          ------
__cpp_lib_format                             ------
__cpp_lib_generic_unordered_lookup           ------
__cpp_lib_int_pow2                           ------
__cpp_lib_integer_comparison_functions       ------
__cpp_lib_interpolate                        ------
__cpp_lib_is_constant_evaluated              ------
__cpp_lib_is_layout_compatible               ------
__cpp_lib_is_nothrow_convertible             ------
__cpp_lib_is_pointer_interconvertible        ------
__cpp_lib_jthread                            ------
__cpp_lib_latch                              ------
__cpp_lib_list_remove_return_type            ------
__cpp_lib_math_constants                     ------
__cpp_lib_polymorphic_allocator              ------
__cpp_lib_ranges                             ------
__cpp_lib_remove_cvref                       ------
__cpp_lib_semaphore                          ------
__cpp_lib_shared_ptr_arrays                  201611
__cpp_lib_shift                              ------
__cpp_lib_smart_ptr_for_overwrite            ------
__cpp_lib_source_location                    ------
__cpp_lib_span                               ------
__cpp_lib_ssize                              ------
__cpp_lib_starts_ends_with                   ------
__cpp_lib_string_view                        ------
__cpp_lib_syncbuf                            ------
__cpp_lib_three_way_comparison               ------
__cpp_lib_to_address                         ------
__cpp_lib_to_array                           ------
__cpp_lib_type_identity                      ------
__cpp_lib_unwrap_ref                         ------

C++23 CORE
__cpp_size_t_suffix                          ------

C++23 LIB
__cpp_lib_is_scoped_enum                     ------
__cpp_lib_stacktrace                         ------
__cpp_lib_stdatomic_h                        ------
__cpp_lib_string_contains                    ------

ATTRIBUTES
carries_dependency                           ------
deprecated                                   201309
fallthrough                                  201603
likely                                       201803
maybe_unused                                 201603
nodiscard                                    201907
noreturn                                     200809
no_unique_address                            201803
unlikely                                     201803

CentOS 7.9默认GCC版本支持情况

4.8.5这个版本并没有完整支持C++11以及之后的特性

$ cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

$ gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)


$ g++ -std=c++11 test.cpp
$ ./a.out 

C++ GENERAL
__cplusplus                                  201103
__cpp_exceptions                             ------
__cpp_rtti                                   ------

C++11 CORE
__cpp_alias_templates                        ------
__cpp_attributes                             ------
__cpp_constexpr                              ------
__cpp_decltype                               ------
__cpp_delegating_constructors                ------
__cpp_inheriting_constructors                ------
__cpp_initializer_lists                      ------
__cpp_lambdas                                ------
__cpp_nsdmi                                  ------
__cpp_range_based_for                        ------
__cpp_raw_strings                            ------
__cpp_ref_qualifiers                         ------
__cpp_rvalue_references                      ------
__cpp_static_assert                          ------
__cpp_threadsafe_static_init                 ------
__cpp_unicode_characters                     ------
__cpp_unicode_literals                       ------
__cpp_user_defined_literals                  ------
__cpp_variadic_templates                     ------

......

采用gcc官方源安装高版本gcc

$ sudo yum install -y centos-release-scl-rh centos-release-scl
$ sudo yum list | grep gcc
gcc.x86_64                                 4.8.5-44.el7           @base         
gcc-c++.x86_64                             4.8.5-44.el7           @base         
libgcc.x86_64                              4.8.5-44.el7           @base         
compat-gcc-44.x86_64                       4.4.7-8.el7            base          
compat-gcc-44-c++.x86_64                   4.4.7-8.el7            base          
compat-gcc-44-gfortran.x86_64              4.4.7-8.el7            base          
devtoolset-7-gcc.x86_64                    7.3.1-5.16.el7         centos-sclo-rh
devtoolset-7-gcc-c++.x86_64                7.3.1-5.16.el7         centos-sclo-rh
devtoolset-7-gcc-gdb-plugin.x86_64         7.3.1-5.16.el7         centos-sclo-rh
devtoolset-7-gcc-gfortran.x86_64           7.3.1-5.16.el7         centos-sclo-rh
devtoolset-7-gcc-plugin-devel.x86_64       7.3.1-5.16.el7         centos-sclo-rh
devtoolset-7-libgccjit.x86_64              7.3.1-5.16.el7         centos-sclo-rh
devtoolset-7-libgccjit-devel.x86_64        7.3.1-5.16.el7         centos-sclo-rh
devtoolset-7-libgccjit-docs.x86_64         7.3.1-5.16.el7         centos-sclo-rh
devtoolset-8-gcc.x86_64                    8.3.1-3.2.el7          centos-sclo-rh
devtoolset-8-gcc-c++.x86_64                8.3.1-3.2.el7          centos-sclo-rh
devtoolset-8-gcc-gdb-plugin.x86_64         8.3.1-3.2.el7          centos-sclo-rh
devtoolset-8-gcc-gfortran.x86_64           8.3.1-3.2.el7          centos-sclo-rh
devtoolset-8-gcc-plugin-devel.x86_64       8.3.1-3.2.el7          centos-sclo-rh
devtoolset-8-libgccjit.x86_64              8.3.1-3.2.el7          centos-sclo-rh
devtoolset-8-libgccjit-devel.x86_64        8.3.1-3.2.el7          centos-sclo-rh
devtoolset-8-libgccjit-docs.x86_64         8.3.1-3.2.el7          centos-sclo-rh
devtoolset-9-gcc.x86_64                    9.3.1-2.el7            centos-sclo-rh
devtoolset-9-gcc-c++.x86_64                9.3.1-2.el7            centos-sclo-rh
devtoolset-9-gcc-gdb-plugin.x86_64         9.3.1-2.el7            centos-sclo-rh
devtoolset-9-gcc-gfortran.x86_64           9.3.1-2.el7            centos-sclo-rh
devtoolset-9-gcc-plugin-devel.x86_64       9.3.1-2.el7            centos-sclo-rh
devtoolset-9-libgccjit.x86_64              9.3.1-2.el7            centos-sclo-rh
devtoolset-9-libgccjit-devel.x86_64        9.3.1-2.el7            centos-sclo-rh
devtoolset-9-libgccjit-docs.x86_64         9.3.1-2.el7            centos-sclo-rh

$ sudo yum install devtoolset-9-gcc  devtoolset-9-gcc-c++

$ source /opt/rh/devtoolset-9/enable
$ gcc --version
gcc (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.