What is and how to compile in C.

What is and how to compile in C.

What is C?

C is a programming language created by Dennis Ritchie in 1972. It is called a low-level language because there are only a few "small layers" between C and the machine language. C, to this day, is one of the best known and most used programming languages in the industry due to its versatility and usability for creating programs of all kinds.

What is compilation in C language?

In short, the compilation process is to "convert one or more source code files into an executable one".

In C this process it involves 4 main steeps where you go from a source code file to an executable file.

The source code file is what the programmers create.

On the other hand, the executable is binary code that can be executed on the computer so the previously created program can do its functions and objectives.

To convert a source code into an executable we need a compiler and in this case we will talk about GCC, the most popular compiler for C used in Linux.

GCC

GCC is short for GNU Compiler Collection and is one of many C compilers available in the world.

A simple example

Suppose we have the following source code... the classic "Hello World":

/*
* File: hello.c
*/

#include<stdio.h>


int main(int argc, const char *argv[]){
printf("Hello World\n");
return 0;

}

A simple compilation would be, on GNU / Linux systems, the following:

gcc hello.c -o hello

That will generate a binary file called hello, and whose description will be similar to the following

When we execute it, the following would happen:

nacho@chitnisky:/tmp$ ./hello

Hello World

The compilation process has 4 parts

  1. The preprocessing
  2. The compiling
  3. The assembling
  4. The linking


No alt text provided for this image


The preprocessing

The first thing the compiler does is preprocess the source file, that is, interpret all the pre-processing directives that we have used, such as #define, #include, #ifdef, etc... Also, it will eliminate all the comments that we have written in the file.

In the particular case of our hello, it will include the stdio.h file (standard input / output header), and remove the comments.

Let's preprocess our example:

gcc -E hello.c -o hello.i

The "-E" modifier allows you to specify to the compiler (gcc) that it only preprocess, and that the output be written to the hello.i file. The .i extension is generally used for pre-processed files.

Now, hello.i is still source code, but if we look at its content we will find something similar to this:

[....]
extern int pclose (FILE *__stream);


extern char *ctermid (char *__s) __attribute__ ((__nothrow__ , __leaf__));
# 840 "/usr/include/stdio.h" 3 4
extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));


extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;


extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
# 868 "/usr/include/stdio.h" 3 4


# 8 "hello.c" 2


# 9 "hello.c"
int main(int argc, const char *argv[]){


printf("Hello World\n");

return 0;

}


The compiling

The compiler will take the preprocessed file and generate IR (Intermediate Representation) code, producing a ".s" file. That said, other compilers can produce assembly code in this compilation step.

Code to stop the compilation proccess after the compiler

gcc -S hello.c


The assembler

The next step is to compile our code. the result of the compilation is a non-executable binary code, called object code, whose characteristic extension is a ".o" file.

Code to stop the compilation proccess after the assembler

gcc -c hello.c


The linking

The next step to make this object code executable is to link, or "link" the object with the system libraries, the libraries it uses.

By default, after this step, is what happen when the entire command "gcc hello.c" is typed without any options, the compiler will create an executable file called a.out, which we can execute typing "./a .out ” on the command line.

And thats it! thats the way GCC works with a C file.

GCC compilation Step by Step video


要查看或添加评论,请登录

Aaron Valiente的更多文章

社区洞察

其他会员也浏览了