GCC (GNU Compiler Collection) is a powerful and widely-used compiler system that supports several programming languages, including C, C++, Objective-C, Fortran, Ada, and more. It is a free and open-source software toolchain maintained by the GNU Project and is available for various platforms, including Linux, macOS, and Windows.
Here are some key features of GCC:
- GCC is a collection of compilers for various programming languages. It includes compilers for C, C++, Objective-C, Fortran, Ada, and others.
- It supports various optimization options to improve the performance of the generated code.
- GCC supports various platforms and architectures, including x86, ARM, PowerPC, and MIPS.
- It is highly customizable and provides many options to control the compiler’s behavior.
- GCC is free and open-source software released under the GNU General Public License (GPL).
To install GCC on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa using APT package manager, you can use either Ubuntu’s default repository or the third-party LaunchPAD PPA repository ubuntu-toolchain. The following guide will demonstrate installing the GCC compiler on Ubuntu using both repositories.
Table of Contents
Update Ubuntu
Before you begin, update your system to ensure all existing packages are up to date to avoid any conflicts during the installation.
sudo apt update && sudo apt upgrade
Method 1: Install GCC Compiler with Ubuntu Repository
The first recommended option to install GCC is to install either the GCC package directly or the build-essential package containing GCC and many other essential development tools such as make, g++, and dpkg-dev.
To begin the installation, use the following command.
sudo apt install gcc
Or
sudo apt install build-essential
Once installed, verify the installation and check the version using the following command.
gcc --version
Method 2: Install GCC Compiler with PPA
Before installing the GCC compiler on your Ubuntu system, you must install some essential packages. To do this, open your terminal and run the following command:
sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https lsb-release
If you encounter issues importing GPG keys from Launchpad PPAs through the terminal, it may be due to missing directories. You can resolve this issue by executing the following command to create the necessary directories:
sudo gpg --list-keys
This command generates the essential directories required for importing GPG keys. Once the necessary package installations are complete, you can proceed by importing the GPG key necessary to access the repositories with the following command:
sudo gpg --no-default-keyring --keyring /usr/share/keyrings/toolchain-ppa.gpg --keyserver keyserver.ubuntu.com --recv-keys 60C317803A41BA51845E371A1E9377A2BA9EF27F
This command imports the GPG key required to access all the repositories. Upon completion, you should see an output similar to the following:
gpg: keybox '/usr/share/keyrings/toolchain-ppa.gpg' created
gpg: key 1E9377A2BA9EF27F: public key "Launchpad Toolchain builds" imported
gpg: Total number processed: 1
gpg: imported: 1
It is important to note that importing the PPA by Rob Savoury may result in modifying and upgrading multiple system packages, which cannot be easily reversed. This process can introduce instabilities, particularly if you use an LTS release. Therefore, creating a backup before installing is highly recommended to ensure your system’s safety.
To import the PPA, run the following command:
echo "deb [signed-by=/usr/share/keyrings/toolchain-ppa.gpg] https://ppa.launchpadcontent.net/ubuntu-toolchain-r/ppa/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/toolchain-ppa.list
After importing the PPA, update your Ubuntu sources list to reflect the changes made by running the following command in your terminal:
sudo apt update
To install a specific version of the GCC compiler on your Ubuntu system using the Ubuntu ToolChain PPA, use the following commands in your terminal:
- GCC Compiler 12
sudo apt install g++-12 gcc-12
- GCC Compiler 11
sudo apt install g++-11 gcc-11
- GCC Compiler 10
sudo apt install g++-10 gcc-10
- GCC Compiler 9
sudo apt install g++-9 gcc-9
After running the appropriate command for the version you want to install, the GCC compiler will be successfully installed on your Ubuntu system.
Configure Alternative Versions of the GCC Compiler
As a developer or specific user, you may need to install multiple GCC compiler versions. Follow these steps to configure alternative versions of GCC on your Ubuntu system.
First, install the versions of GCC you need. You can install multiple versions of GCC along with G++ using the following command:
sudo apt install gcc-9 g++-9 gcc-10 g++-10 gcc-11 g++-11 g++-12 gcc-12
Once you have installed the necessary versions, use the update-alternatives command to configure the priority of each version. The following example command sets the priority split between GCC 9, GCC 10, GCC 11, and the latest GCC 12.
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 100 --slave /usr/bin/g++ g++ /usr/bin/g++-12 --slave /usr/bin/gcov gcov /usr/bin/gcov-12
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 80 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 60 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 40 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9
The above commands set GCC 12 as the highest priority with a value of 100. However, you can configure the priorities based on your preferences.
To confirm that GCC 12 is the default version on your system, run the following command:
gcc --version
Example output:
You can reconfigure the default GCC version on your system by using the update-alternatives command. First, use the following command to list the priorities you previously set:
sudo update-alternatives --config gcc
Example output:
This command will display a list of installed GCC versions and their priorities. You can then select the default version by entering the corresponding number.
That’s it! You have successfully configured alternative versions of GCC on your Ubuntu system.
Test GCC Compiler: Create a Test Application
To test compiling with GCC, create the famous “Hello World” program in C using any text editor. This tutorial will use nano.
Open the nano text editor and create a new file named hello.c:
nano hello.c
Add the following code to the file:
#include <stdio.h>
int main()
{
printf("Hello, World from Linuxcapable.com!");
return 0;
}
Save the file by pressing CTRL+O, then exit nano by pressing CTRL+X.
To compile the Hello World program, use the following command:
gcc hello.c -o hello
This command compiles the program and generates an executable file named hello
.
Next, run the compiled program by entering the following command:
./hello
You should see the following output in your terminal:
Hello, World from Linuxcapable.com!
Conclusion
In conclusion, installing the GCC compiler on Ubuntu is a straightforward process that can be accomplished using the APT package manager with Ubuntu’s default repository or the third-party LaunchPAD PPA repository. Following the steps outlined in this guide, you can easily install a specific GCC compiler version or configure alternative versions.
Once installed, GCC can compile and run C and C++ programs on your Ubuntu system. With the addition of the manual pages package, you can also access comprehensive documentation on how to use GCC and its various features. Whether a novice or an experienced developer, having GCC installed on your Ubuntu system is essential for developing and running C and C++ programs.
For further reading and information, visit the GCC official documentation.
FAQ on GCC Compiler with Ubuntu
Q: What is GCC, and why is it important for Ubuntu users?
A: GCC (GNU Compiler Collection) is a free and open-source compiler system that supports various programming languages, including C, C++, and Fortran. It is essential for Ubuntu users as it allows them to compile and run programs on their systems. GCC is also highly portable and runs on many different platforms.
Q: Can I use a different version of GCC than Ubuntu?
A: Yes, Ubuntu allows users to install and use different versions of GCC using the APT package manager or the LaunchPAD PPA repository. By configuring the priority of each version using the update-alternatives command, users can choose which version of GCC to use as the default.
Q: How do I troubleshoot errors when compiling code with GCC on Ubuntu?
A: There are several ways to troubleshoot errors that occur during code compilation with GCC on Ubuntu. Firstly, users should carefully review the error message output and look for syntax errors or missing dependencies. They can also use debugging tools like GDB to identify and fix errors. Additionally, checking the GCC documentation and online forums for solutions to common errors can be helpful.
Q: What are the advantages of using GCC over other compilers on Ubuntu?
A: GCC is highly optimized, supports multiple programming languages, and is available for free. It also has a large user community and is highly portable, making it an excellent choice for developers who need to write code that runs on multiple platforms.
Q: How do I link external libraries to my GCC compiled program on Ubuntu?
A: Users can link external libraries to their GCC compiled programs using the -l flag followed by the library’s name. For example, to link the math library, users would use the following command: gcc -o myprogram myprogram.c -lm
Q: Can GCC compile programs written in languages other than C and C++ on Ubuntu?
A: Yes, GCC supports several other programming languages, including Fortran, Ada, and Objective-C. Users can install the necessary language-specific libraries and compilers using the APT package manager or LaunchPAD PPA repository.
Q: What are some best practices for optimizing code compiled with GCC on Ubuntu?
A: Some best practices for optimizing code compiled with GCC on Ubuntu include using appropriate optimization flags, avoiding unnecessary function calls, and minimizing the use of global variables. Additionally, profiling tools like Gprof can help identify areas of code that could benefit from optimization.
Q: How do I cross-compile code for a different architecture using GCC on Ubuntu?
A: To cross-compile code for a different architecture using GCC on Ubuntu, users must first install the appropriate cross-compilation tools and libraries for the target architecture. They can then use the appropriate GCC cross-compiler toolchain and modify their code to be compatible with the target architecture.
Q: What are the limitations of using GCC on Ubuntu compared to commercial compilers?
A: While GCC is a powerful and highly optimized compiler, it may not always be as efficient or feature-rich as commercial compilers. Additionally, some hardware-specific optimizations may not be available in GCC, and support for certain programming languages or libraries may be limited. However, GCC is a highly capable and widely used compiler for most general-purpose programming tasks.