GCC Linux
GCC Linux
In Linux, the GCC stands for GNU Compiler Collection. It is a compiler system for the various programming languages. It is mainly
used to compile the C and C++ programs. It takes the name of the source program as a necessary argument; rest arguments are
optional such as debugging, warning, object file, and linking libraries.
GCC is a core component of the GNU toolchain. Various open-source projects are compiled using the GCC, such as Linux kernel and
GNU tools.
It is distributed under the GPL (General Public License). The first version, GCC 1.0, was released in 1987. It was only for the C
programming language, but in the same year, it was extended for the C++ programming language. Later, it was developed for other
programming languages such as Objective-C, Objective-C++, Fortran, Java, Ada, Go, and more. Its latest version has the much-
improved implementation of the programming languages.
It is the official partner of GNU OS; therefore, it has been adopted as the standard compiler of the Linux based systems.
PlayNext
Unmute
Duration 18:10
Loaded: 12.11%
Â
Fullscreen
Backward Skip 10sPlay VideoForward Skip 10s
Components of GCC
GCC is a portable tool, and it can run on many operating systems. Also, it can be ported to Windows by using some tools such
as Cygwin, MinGW, and MinGW-W64. As it is a key component of GNU toolchain, it contains the following components for
developing applications and operating systems:
GNU Compiler Collection (GCC): It is a compiler suite for many languages, such as C/C++ and Objective-C/C++.
GNU Make: It is an automation tool that is used to compile and build applications.
GNU Binutils: It is a suite that contains binary utility tools such as linker and assembler.
GNU Autotools: It is a build system that contains some utility tools such as Autoconf, Autoheader, Automake, and Libtool.
The GCC compiles and makes executable a program in the following four steps:
Pre-processing: In this step, the GNU preprocessor (cpp.exe) creates an intermediate file having the source code. The preprocessor
includes the headers (#include) and expands the macros (#define). Consider the below command:
Compilation: In this step, the pre-processed source code is compiled into assembly code for a specific processor.
1. gcc -S hello.i
The above command will create an assembly file 'hello.s.' The '-s' option is used to specify that to produce the assembly code rather
than object code.
Assembly: The assembly code is converted into machine code. It will create an object file 'hello.o.'
1. as -o hello.o hello.s
Linker: The Linker (ld.exe) is the final step of the compiler process. It links the object code with the library code and produces an
executable file.
1. ld -o hello.exe hello.o
By default, it comes with the most Linux distributions. We can verify it by executing the below command:
1. gcc -version
The above command will display the installed version of the GCC tool. If it is not installed, follow the below steps to install it:
It will ask for the system administrative password, enter the password. It will start updating the system package. Consider the below
snap of output:
It contains various packages such as gcc, g++, and make utility. Execute the below command to install it:
The above command will install the required packages for the GCC utility. Now, we can use the GCC utility in our machine. Consider
the below snap of output:
Step3: Verify the installation.
ADVERTISEMENT
1. gcc --version
It will display the installed version of GCC utility. To display the more specific details about the version, use the '-v' option. Consider
the below output:
Here, we have successfully installed the GCC utility. Let's understand to use it. We will create and execute some c programs using
GCC.
Create a basic c program "Hello world!". Create a file 'hello.c" and put below code in it:
1. #include <stdio.h>
2. int main() {
3. printf("Hello, world!\n");
4. return 0;
5. }
1. gcc hello.c
If we directly run the hello.c, it will throw the error. Make it executable, the default executable file for the Linux system is a.out. To
execute the file, execute the chmod command as follows:
1. chmod a+x a.out
1. ./a.out
ADVERTISEMENT
By default, the gcc command creates the object file as 'a.out.' If you want to change the default output file name, use the '-
o' option.
1. gcc hello.c
The above command will generate the object file 'a.out.' To specify the object file name, execute the command as follows:
It will generate the output file 'hello.' Consider the below output:
To enable all warnings in the output, use the '-Wall' option with the gcc command. Let's create a variable in the main function of
hello.c. Consider the below code:
hello.c:
1. #include <stdio.h>
2. int main() {
3. int a;
4. printf("Hello, world!\n");
5. return 0;
6. }
If we compile the above code using -Wall option. It will throw the warnings. Execute the below command to compile the file:
The above command will display the warnings. Consider the below output:
We can produce only the preprocess output by using the '-E' option. Consider the below command:
From the above command, a file 'hello.i' that contains preprocessed output is generated. Consider the below output:
To produce the assembly code, execute the command with the '-S' option. Consider the below command:
The above command will generate the 'hello.s.' file that contains the assembly code. Consider the below output:
We can produce only the compiled code by using the '-C' option. Consider the below command:
1. gcc -C hello.c
The above command will generate a file 'a.out,' having only the machine code or compiled code. Consider the below output:
We can produce all the intermediate files of the compilation process by using the '-save-temp' option. Consider the below output:
The above command will generate all the intermediate files as well as executable files at once. Consider the below output:
We can display verbose information of every step taken by gcc command. To do so, execute the command with the '-v' option.
1. gcc -W -v hello.c
ADVERTISEMENT
We have discussed some most useful examples of the gcc command. Since the gcc command facilitates with a huge number of
options, you can get stuck anywhere while using it. Let's see how to get help by yourself from the terminal.
GCC manual
If you get stuck anywhere while using the gcc command, you can take help from your terminal. To access the manual from the
command line, execute the man command as follows:
1. man gcc
The above command will display the manual that contains a description and list of the supported options with a brief description.
Consider the below snap of output:
you can scroll the output to read more. Press the 'h' key for help and q to exit from the manual and back to terminal.