How to compile a source code?
Publisher: Psychz Networks, August 03,2021A source code, also known as a program, is created using a programming language to make it easy for humans to code. These programs need to be translated into machine language to execute them (computer understandable language). This translation is known as compilation or interpretation.
While code written in any programming language must be parsed, some codes are compiled, and some are interpreted.
Code written in languages used for software development such as C, C++, Java, etc., is compiled. These languages are designed to create executable programs, so the code written in them needs to be compiled. Code written in languages used for web development such as Javascript, PHP, VBScript, etc., is interpreted. These languages are designed to create web applications and display data on web pages, not to create executable programs. So code written in them needs to be interpreted.
What is a Compiler?
A compiler is a unique program that processes statements written in a particular programming language and turns them into machine language or "code" that a computer's processor uses. Typically, a programmer writes language statements in a language such as C, C++, or Java one line at a time using an editor. The file that is created contains what are called the source statements. The programmer then runs the appropriate language compiler, specifying the file's name that contains the source statements.
Role of a compiler
A compiler takes the program code (source code) and converts the source code to a machine language module (called an object file). Another specialized program, called a linker, combines this object file with other previously compiled object files (in particular run-time modules) to create an executable file. So, the conversion from source code to machine-executable code takes place before the program is run.
Following are the two steps involved in the compilation:
1. Source code is turned into object code by a compiler
A compiler turns source code into object code, but it is not ready to become a program. Before object code can become a program, it has to pass through a linker.
2. Object code is passed through a linker.
A linker is a program that combines various modules and objects code files into an executable program. Once the data is passed through a linker, an executable program comes into existence.
How to compile?
Now that you have a fair understanding of what a compiler is and how it works let us now understand how you can compile a source code.
There are various free and open-source compilers available in the market, which you can download as per the programming language you have used to write the code. Compilers are also OS-dependent, and hence you will have to choose accordingly. The most popular and largely used is the GCC developed by the GNU project. It is compatible on most of the operating systems which makes it so popular among the coders. Let us understand more about GCC and its functionalities
GCC is an integrated collection of compilers for several major programming languages, which as of this writing are C, C++, Objective-C, Java, FORTRAN, and Ada. The GNU compilers all generate machine code, not higher-level language code, translated via another compiler.
There are four steps to the compilation process: Preprocessing, Compiling, Assembly, and Linking. Here's how each of the process works in detail
Preprocessing
GCC is capable of preprocessing and compiling several files either into several assembler input files, or into one assembler input file.
Let us see how it works by running a small piece of code in C language.
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return (0);
}
Note: For example purpose, we will save the file name as "mycode.c"
Preprocessing is the first step. The preprocessor obeys commands that begin with # (known as directives) by:
- removing comments
- expanding macros
- expanding included files
If you included a header file such as #include <stdio.h>, it will look for the stdio.h file and copy the header file into the source code file.
Compiling
The usual way to compile a program is to run it along with gcc
# gcc mycode.c
Note: Your present working directory should be same as your code file directory.
This step takes the output of the preprocessor and generates assembly language, an intermediate human readable language, specific to the target processor.
Assembly
Assembly is the third step of compilation. The assembler will convert the assembly code into pure binary code or machine code (zeros and ones). This code is also known as object code.
Linking
Linking is the final step of compilation. The linker merges all the object code from multiple modules into a single one. If we are using a function from libraries, linker will link our code with that library function code. In static linking, the linker makes a copy of all used library functions to the executable file. In dynamic linking, the code is not copied, it is done by just placing the name of the library in the binary file.
I hope you have found this article interesting and helpful in understanding and choosing the suitable compiler for your development project. Would you please vote for this article if you have found it helpful?