Compiling C File With gcc
Published: 2022-09-16 07:02:36

## **What is c programming**
C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system. The main features of the C language include low-level memory access, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development. 
For C to be executable it must first be compiled


## **Introduction to gcc**
The original author of the GNU C Compiler (GCC) is Richard Stallman,the founder of the GNU Project.
gcc stands for GNU compiler collections which is used to compile mainly c and c++
To check gcc version present in you system use the following command
`gcc -v`
**syntax**
`gcc [-C | -S | -E |] [-std = standard]` 
**Compilation**
Compilation refers to the process of converting a program from the textual source code, in a programming language such as C or C++, into machine code, the sequence of 1’s and 0’s used to control the central processing unit (CPU) of the computer. This machine code is then stored in a file known as an executable file, sometimes referred to as a binary
file.

A compiler is a computer program that translates the source code written in a high-level programming language(i.e C) to a low-level language such as machine code.

**The compilation process**
There are four main steps involved in the compilation process as mentioned below:
1. Preprocessing
2. Compiling
3. Assembling
4. Linking

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jk1uxd2gcq9qyly0yrj.PNG)
**Preprocessing**
This is the first phase through which the source code is passed.

- This phase includes:
- Removal of comments
- Expansion of included files
- Conditional combination
- inclusion of  the code from the header files
Its output is stored with a file extesion of `.i`

To view the output of the pre-processing stage, we can use the command `gcc -E filename.c` option as shown below.

```
root@DESKTOP-TI71NAL:~# gcc -E helloworld.c
```
**Compiler**
In this phase compilation of the `filename.i` happens in order to compile an output file of filename which is in an assembly level
`username@hostname:~$ gcc -S filename.c`
**Assembler**
Here is where `filename.s` is taken as input and turned into `filename.o` by the assembler.

```
gcc -c filename.c
```

**Linker**
This the final phase in which all the linking  of function call with their definitions are done.
`gcc` by default does dynamic linking and so `printf()` is dynamically linked.
The tasks performed by the linker include:

- Linking together all the object files from the different source files.
- Linking function calls with their definitions. The linker knows the locations of the function definitions in the static or dynamic libraries.
**Option 1**

Run the following command:

```
username@hostname:~$ gcc helloworld.c
```
When you run this command, the compiler generates an executable program called a.out. To run the executable program type the following command:

```
username@hostname:~$ ./a.out
```
**Option 2**
To give the executable program a different name, we can add the "-o" option to the gcc command after the name of the file we are compiling, as shown below:

```
username@hostname:~$ gcc helloworld.c -o helloworld
```

To run the executable program, use the command below:

```
username@hostname:~$ ./helloworld
```

**## gcc options**

`-o` this option is used for output
      this will compile the filename.c , but instead of giving the default name it is executed using ./filename

```
gcc filename.c -o filename
```

`-werror` This compiles the source and show the warning if any error is present in the program.

```
gcc filename.c -werror -o filename
```
`-wall` this not only checks the errors but any other kind of warning

```
gcc filename.c -wall -o filename
```

```
Thanks for reading
```