GCC stands for GNU Compiler Collection, which is a highly optimizing and extensive compilation suite. It offers packages for many programming languages, including C++, C, Ada, Fortran, Objective-C, and Go. The GCC compiler in Debian 12 is very important for developers and programmers who work in the Linux operating system.
The GCC installed in Debian 12 is capable of compiling software from source. This is vital for programmers needing their code to be transformed into a binary program or an application. It is used for making a library out of the code they have written, as well as when repurposing code found in the wild.
In this article, we will describe how you can install the GCC compiler on your Debian 12.
Table of Contents
- How to Install GCC Compiler on Debian 12?
- How to Run a C or C++ Program using the GCC Compiler?
- Conclusion
Let’s start the article with installation.
How to Install GCC Compiler on Debian 12?
GCC compilers provide the ability for users to build and install open source software, such as the Linux operating system distribution, from source without needing to install any software in advance. For installation, update the package lists of your computer and then install the GCC package with“sudo apt install build-essential”.
Here’s how you can install the GCC compiler on Debian 12:
Method 1: Installing GCC Compiler on Debian 12 Using APT Package Manager
With GCC, however, they can ensure that they are compatible with the system libraries. Using the APT package manager, the installation of the GCC Compiler on Debian 20.04 becomes easy. Let’s follow the steps below:
Step 1: Updating Package list
First, refresh the package index and upgrade the system by issuing the following apt command:
sudo apt update

Step 2: Install GCC Package
Once it is done, install the GCC package with the use of the “build-essential” tool:
sudo apt install build-essential

It installs the GCC compiler and other utilities like “make”, and “g++”.
Step 3: Check GCC Version
Now users can even check the installation of GCC by checking its version:
gcc --version

Remove/Uninstall GCC Compiler
To uninstall/remove GCC Compiler on Debian 12, use the “autoremove” option with “build-essential” packages as shown below:
sudo apt autoremove build-essential

This is how you install the GCC compiler on Debian 12.
Method 2: Installing GCC Compiler on Debian 12 Using Source Code
The installation of GCC compilers on Debian 12 provides a foundation that enables users to develop, customize, and learn software, creating a space where code can be turned into viable and effective software tools.
To install GCC Compiler on Debian 12 From Source Code, use the following steps:
Step 1: Updating the Packages List
Start by making sure your system software is up to date. (Package installation required for the system):
sudo apt update

Step 2: Install Necessary Dependencies
Then, install the dependencies, along with the “build-essential” tool to build the code if there are any problems.
sudo apt install build-essential

Step 3: Download the GCC Source Code
Then, download the GCC source from the official GNU website using the “wget” (HTTP client) tool:
wget https://ftp.gnu.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.gz

Step 4: Extract the Downloaded File
So now you can uncompress the downloaded file with the “tar” command using the “xvf” option:
tar -xvf gcc-13.2.0.tar.gz

Step 5: Configure Build Environment
Then, go to that extracted directory with the “cd” command, set up your build environment configure the” command as below:
cd gcc-13.2.0
../gcc-13.2.0/configure --prefix=$HOME/gcc-13.2.0-install --disable-multilib --enable-languages=c,c++

If users get an error about dependencies, install these tools to fix it:
./contrib/download_prerequisites

Step 6: Compile Source Code
Users will have built the environment and then can compile the source code using the command “make” :
make -j$(nproc)

Step 7: Install GCC
At last, install GCC and required packages using the “install” tool as follows:
make install

Step 8: Verify GCC Installation
To verify, users can verify the GCC version using the path below:
/usr/local/gcc-13.2.0/bin/gcc-13.2.0 --version
This is all about how to install the GCC Compiler on Debian 12 from the source code.
How to Run a C or C++ Program Using the GCC Compiler?
Understanding how to compile code in the software development process and how programs behave on a Unix-like operating system like Debian is a solid foundation.
Here is how to write and compile a simple C or C++ program using the GCC compiler in Debian 12:
Step 1: Install the Build-Essential Package
First, you have to install this package, which includes the GCC compiler and other tools:
sudo apt install build-essential
Step 2: Create a C Program File
For example, to make a file called “linux_file.c”, and then enter a text editor, you can use “nano” (the user may give it any name to your program):
nano linux_file.c
To print the message, type the following in the editor in C source code:
#include <stdio.h>
int main() {
printf("Linux has Debian 12 distribution");
return 0;
}

Now, save the file and close the text editor, then go back to the terminal.
Step 3: Compile a C Program
Now you should use the “cd” command to browse through the folder where the C or C++ has been saved. To build a program, you use the “gcc” command. It will make a binary called “debia12”
gcc -o debian12 linux_file.c

In this case, debian12 is the name that you want to call your compiled program, whereas “linux_file.c” is the source file:
For a C++ program, use g++ along with the source file name with a .cpp extension:
g++ -o output_name source_file.cpp
Note: Don’t forget to replace “output_name” and ‘source_file’ with your program’s name and file.
Step 4: Run the Compiled Program
Last of all, users launch the program ‘genie’ in the following manner:
./debian12

That’s all for a simple C++ program.
Conclusion
For Debian 12 to install the GCC Compiler, first update the package lists and install the package using the command “sudo apt install build-essential”. It doesn’t just install the GCC, but also includes other programs that are part of it, like “g++” and “make”. Or users can also install GCC on Debian with the source code.
If you want to compile the C++ program with the “gcc -o output_name source_file.c” command. Here “output_name” is the name you want to store your compiled program with and “source_file. c” is your source file.
Leave feedback about this