The GCC 16.2 compiler is out, and I couldn’t wait to get my hands on it. I have just now installed GCC 16.2 on my Ubuntu machine, quickly compiled a C++26 program, and ran it, and I was excited to see it went like a charm. So I have jotted down the steps I followed to install the new compiler and a sample program written using C++26 feature reflection, compiling and running it on my Ubuntu machine. Ok, let’s get started then.
UPDATE (September 2026): I first wrote this post for GCC 16.1 and have now updated it for GCC 16.2. If you already built 16.1 from this post, use a new, empty build folder for 16.2. Reusing the old one makes configure stop with a “has changed since the previous run” error.
Installing GCC 16.2 on Ubuntu
Installing the dependencies
Open a terminal in Ubuntu and run the following command in the terminal:
sudo apt updatesudo apt install -y build-essential wget tar flex bison texinfo libgmp-dev libmpfr-dev libmpc-dev zlib1g-dev
Once the dependencies are installed we can then go ahead and download the GCC 16 tarball.
Downloading the GCC 16.2 Source Code
wget https://ftp.gnu.org/gnu/gcc/gcc-16.2.0/gcc-16.2.0.tar.xz
tar -xf gcc-16.2.0.tar.xz
cd gcc-16.2.0
./contrib/download_prerequisites
WARNING! Once this is done come out of the source code folder gcc-16.2.0 because trying to compile it inside the source code folder will cause issues. I made the mistake.
So create a separate folder and follow the instructions as shown below:
cd ~
mkdir gcc-16.2-build
cd gcc-16.2-build
Configure the make environment:
../gcc-16.2.0/configure \
--prefix=/opt/gcc-16.2 \
--enable-languages=c,c++ \
--disable-multilib
Then build the source code:
make -j$(nproc)
WARNING! Building the source code took me ages on my Ubuntu desktop, so beware about the time it takes to build the source code as well as it significantly slowed down my system.
Once the building is done, install it:
sudo make install
Once the installation is done check the version as follows:
/opt/gcc-16.2/bin/gcc --version
/opt/gcc-16.2/bin/g++ --version
gcc (GCC) 16.2.0
Copyright © 2026 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.
g++ (GCC) 16.2.0
Copyright © 2026 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.
Set the Path
export PATH=/opt/gcc-16.2/bin:$PATH
export LD_LIBRARY_PATH=/opt/gcc-16.2/lib64:$LD_LIBRARY_PATH
The PATH setting makes g++ pick up the new compiler. The LD_LIBRARY_PATH setting matters when you run the programs: anything built with GCC 16 that uses new library features such as std::println needs the libstdc++ from /opt/gcc-16.2/lib64, because the one that comes with Ubuntu is older. Without it the program fails to start with a “GLIBCXX_… not found” error.
These two exports only last for the current terminal. To keep them for every new terminal, add them to your ~/.bashrc:
echo 'export PATH=/opt/gcc-16.2/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/opt/gcc-16.2/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
Ok, that’s it about the installation of our new guest GCC 16.2. Now time to put it to the test.
Compiling your first C++26 Program
I have written a sample C++26 program using C++26’s feature reflection which is as follows:
// test_reflection.cpp <meta> <print>struct SensorReading{ int id; double temperature;};int main(){ constexpr auto reflected_type = ^^SensorReading; std::println("C++26 reflection test compiled successfully."); return 0;}
Compile the Program
Compile the program using the below:
g++ -std=c++26 -freflection test_reflection.cpp -o test_reflection
GCC 16 needs the -freflection flag on top of -std=c++26 for reflection. If you leave it out, the compiler stops with:
error: reflection is only available with '-freflection'
Run the Program
~$ ./test_reflection C++26 reflection test compiled successfully.
Yay! That’s our brand new C++26 program running on Ubuntu! Time for you to try now!
My Installation Environment: ~$ uname -a
Linux vbhadra-DQ77MK 6.8.0-110-generic #110~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 27 12:43:08 UTC x86_64 x86_64 x86_64 GNU/Linux
C++26 Series
Follow my C++26 series as I explore the language’s new features through practical examples.
- 01 Compile Your First C++26 Program with GCC 16.2
-
02
C++26: What Is
template for? - 03 C++26: What Is Reflection and How Do You Use It?
- 04 C++26 Reflection: Simplifying JSON Serialization
- 05 C++26 Reflection Annotations: Automated Member Validation
- 06 C++26 Contracts: What Do They Add Beyond Manual Checks and Assertions?
Discover more from Tech For Talk
Subscribe to get the latest posts sent to your email.
6 Comments