You can compile C programming files with GCC, Clang, and other software. However, most students prefer GCC over other software since it provides C programming assignment help to compile programs quickly, is ready to run, and takes up less memory. So, before I dive straight into the compilation steps, let’s give a glimpse of the GCC progra3m if it is the first time you hear the name –GCC. 

Well, GCC is free software under the GNU General Public License – an open-source (GPL) compiler found on various systems, ranging from GNU/Linux to every flavor of UNIX to Windows. Richard Stallman, in 1983, launched h program.

offering Java programming assignment help developers to have access to powerful tools for free. It supports many languages like C, C++, Fortran, and highly portable, and produces good code. Apart from that, you can also use it as a cross-compiler to support new technologies quicker.

To explain all the compilation steps, I first must clarify a few programming concepts beforehand. 

The C programming language

Developers write all the software, programs, websites, and apps in a specific programming language. Fundamentally, everything we see on the screen of our laptop or smartphones are just a compilation of many code engineers write in different languages. But, of course, every programming language has another use, and here we will focus on C.

Dennis Ritchie invented the C programming language first used1972. It is a low-level language, with the only minor difference between C and machine language so that you may consider it closer to the computer’s hardware. C is also a compiled language, as against interpreted. It means the source files you write in C should be assembled to make them executable.

The tools

Before anything else, let’s talk about the tools I have used below in the example. I will work on a Unix-like operating system, so the procedures may vary if you try it on Windows. I have access to the shell -“a program that takes commands from the keyboard and gives them to the operating system to perform” as mentioned  For this, I must have a terminal or terminal emulator, which is just a window that facilitates interaction with the shell. Inside the terminal lies the shell prompt, which consists of my user name and the name of the machine, tagged along with a PS1 environment variable that is often the character “$”. I can input commands after this character in what I call the command line. I also need a text editor, like vi or emacs, to create a source file.

Compilation

The compilation is the translation of source code (the code we write) into object code (sequence of statements in machine language).

The compilation process has four different steps:

  • The preprocessing
  • The compiling
  • The assembling
  • The linking

The compiler we have used in this example is GCC which stands for GNU Compiler Collection.

To use it, we should ensure that we install it on our computer if it’s not already there.

The source code

For our example, let’s take a look at a source code inside a file called “main. c”, where “.c” is a file extension that usually means the file is written in C. 

You will find that the preprocessor directive # includes that tells the compiler has the studio.h header file that appears in pink, but I will come back to it later.

Comments about the code come in blue. It would help if you remembered what your code does months after having created it. I don’t need them in such a small program, but it’s good practice to put them.

Next, I have my entry point, the main() function. It means the program will start by executing the statements inside this function’s block, which lies between the curly brackets. I can include only two statements: one that will print the sentence “Hello, World” on the terminal, and another one that tells the program to “return” 0 if it exited, or ended, correctly. So once I have compiled it, if I run this program, I will find the phrase “Hello, World” appearing. 

For the main. C code to be executable, I need to enter the command “GCC main. c”, and the compiling process will go through all of the four steps it contains. Of course, GCC has options that allow me to stop the compiling process after each step. Let’s take a look at them.

The steps

  • The preprocessor

The preprocessor has several roles:

  • It gets rid of all the comments in the source file(s)
  • It includes the code of the header file(s), a file with extension .h that contains C function declarations and macro definitions.
  • It replaces all macros (fragments of code that have been given a name) with their values.
  • The output of this step will be stored in a file with a “.i” extension, so here it will be in the main. i.

To stop the compilation right after this step, we can use the option “-E” with the GCC command on the source file and press enter.

  • The compiler

The compiler will take the preprocessed file and generate IR code (Intermediate Representation), producing a “.s” file. That being said, other compilers might have assembly code at this step of compilation.

We can stop after this step with the “-S” option on the GCC command and press enter.

  • The assembler

The assembler takes the IR code and transforms it into object code in machine language (i.e. binary). This will produce a file ending in “.o”.

After this step, you can stop the compilation process by using “-c” with the GCC command and pressing enter.

  • The linker

The linker creates the final executable, in binary, and can play two roles:

  • Linking all the source files together is all the other object codes in the project. For example, if I want to compile main. C with another file called secondary. C and make them into one single program; this is where the object code of secondary. C (that is secondary. o) will be linked to the main.c object code (main. o).
  • Linking function calls with their definitions. The linker knows where to look for the function definitions in the static libraries or dynamic libraries. Static libraries are “the result of the linker making a copy of all used library functions to the executable file”, according to geeksforgeeks.org, and dynamic libraries “don’t require the code to be copied; it is done by just placing the name of the library in the binary file”. Note that GCC uses by default dynamic libraries. So, in this example, the linker will find the definition of my “puts” function and link it.

By default, after this fourth and last step, when I type the whole “GCC main. C” command without any options, the compiler will create an executable program called a.out, which we can run by typing “./a.out” in the command line.

I can also choose to create an executable program with the name I want by adding the “-o” option to the GCC command, placed after the name of the file or files we are compiling, and pressing enter:

So now I can either type “./a.out” if you didn’t use the -o option or “./my_program” to execute the compiled code, the output will be “Hello, World”, and following it, the shell prompt will appear again.

Final Words:

The GNU Compiler Collection offers many more great tools to compile and operate our programs that would deserve to write about. Still, this article was about the basic steps of compilation. Maybe next time!

Author Bio: Harvey Allen is an information technology engineer at a reputed company in London. He also supervises the c programming assignment help service at MyAssignmenthelp.com.

Leave a Reply

Your email address will not be published.