0% found this document useful (0 votes)
6 views

GCC Linux

GCC is a compiler system used to compile C and C++ programs in Linux. It is distributed under GPL and contains components like GCC, Make, Binutils, and Debugger. It compiles programs in four steps - preprocessing, compilation, assembly, and linking. GCC can be installed on Linux and is used to compile open source projects like the Linux kernel.

Uploaded by

rohan740.be22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

GCC Linux

GCC is a compiler system used to compile C and C++ programs in Linux. It is distributed under GPL and contains components like GCC, Make, Binutils, and Debugger. It compiles programs in four steps - preprocessing, compilation, assembly, and linking. GCC can be installed on Linux and is used to compile open source projects like the Linux kernel.

Uploaded by

rohan740.be22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

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

Current Time 1:11

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 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:
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:

1. cpp hello.c > hello.i

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.

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

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:

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:

Step1: Update the package list.

To update the package list, execute the following command:

1. sudo apt update

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

Step2: Install the build-essential package.

It contains various packages such as gcc, g++, and make utility. Execute the below command to install it:

1. sudo apt install build-essential

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

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

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.

Run first C program by 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. }

Now, compile the hello.c as follows:

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

Now, run the c program as:

1. ./a.out

Consider the below output:

ADVERTISEMENT

GCC command Examples

Some useful examples of gcc command are as following:

o 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:

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:

1. gcc hello.c -o hello

It will generate the output file 'hello.' Consider the below output:

o 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:

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:

1. gcc -wall hello.c

The above command will display the warnings. Consider the below output:

o 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:

1. gcc -E hello.c > hello.i

From the above command, a file 'hello.i' that contains preprocessed output is generated. Consider the below output:

Produce the assembly code

To produce the assembly code, execute the command with the '-S' option. Consider the below command:

1. gcc -S hello.c > hello.s

The above command will generate the 'hello.s.' file that contains the assembly code. Consider the below output:

Produce the compiled code

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:

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:

1. gcc -save-temps hello.c

The above command will generate all the intermediate files as well as executable files at once. Consider the below output:

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

1. gcc -W -v hello.c

Consider the below snap of output:

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.

You might also like