Javatpoint Logo
Javatpoint Logo

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.

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 Debugger (GDB). It is used to debug the applications.

GNU Autotools: It is a build system that contains some utility tools such as Autoconf, Autoheader, Automake, and Libtool.

GNU Bison: It is a parser generator as lex and yacc.

GCC Compiler Process

The GCC compiles and makes executable a program in the following four steps:

GCC Linux

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:

The above command will create an intermediate file 'hello.i.'

Compilation: In this step, the pre-processed source code is compiled into assembly code for a specific processor.

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.'

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.

The above command will create an executable file 'hello.exe.'

Installation of GCC on Linux

By default, it comes with the most Linux distributions. We can verify it by executing the below command:

The above command will display the installed version of the GCC tool. If it is not installed, follow the below steps to install it:

Step1: Update the package list.

To update the package list, execute the following command:

It will ask for the system administrative password, enter the password. It will start updating the system package. Consider the below snap of output:

GCC Linux

Step2: Install the build-essential package.

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:

GCC Linux

Step3: Verify the installation.

To verify the installation, execute the gcc -version command as follows:

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:

GCC Linux

Here, we have successfully installed the GCC utility. Let's understand to use it. We will create and execute some c programs using GCC.

Run first C program by gcc

Create a basic c program "Hello world!". Create a file 'hello.c" and put below code in it:

Now, compile the hello.c as follows:

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:

Now, run the c program as:

Consider the below output:

GCC Linux

GCC command Examples

Some useful examples of gcc command are as following:

  • Specify the object file name

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.

Let's execute the basic gcc command:

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:

GCC Linux
  • Enable all Warnings

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:

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:

GCC Linux
  • Produce the stepwise output of the compilation process

We can only produce the stepwise output of the compilation process.

Produce the preprocessor 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:

GCC Linux

Produce the assembly code

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:

GCC Linux

Produce the compiled code

We can produce only the compiled code by using the '-C' option. Consider the below command:

The above command will generate a file 'a.out,' having only the machine code or compiled code. Consider the below output:

GCC Linux

Produce all the intermediate files of the compilation process

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:

GCC Linux
  • Print the output verbosely.

We can display verbose information of every step taken by gcc command. To do so, execute the command with the '-v' option.

Consider the below snap of output:

GCC Linux

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:

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:

GCC Linux

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.


Next TopicLinux Error Codes




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA