Compilation process in c.

 

Compilation process in c.

馃厭How do we compile in c?

Compilation stages:

Normally, when programming we do not create a single C .c file, but rather several of them containing different program functions. This provides us with several advantages: better code organization, better modularity and, above all, more ease (and speed) when compiling.


This outline can be used to list common programming tasks. The most common task is generating an executable program. As its name indicates, it is a file that contains code that is directly executable by the processor. This can be built in various ways:

1. Preprocessed.

At this stage the directives to the preprocessor are interpreted. Among other things, variables initialized with #define are substituted in code for their value in all places where their name appears.











2. Compilation.

The compilation transforms the C code into the assembly language of the processor of our machine.

The compilation transforms the C code into the assembly language of the processor of our machine.


3. Assembled.

Assembly transforms the assembly language program into object code, a machine language binary file executable by the processor.

The assembler is named like this: 


All in one step!!!

In a program with a single source file, all the above process can be done in a single step:

$ gcc -o circle circle.c

The file circle.o is not created; intermediate object code is created and destroyed without the operator seeing it, but the executable program appears there and works.

It is instructive to use the gcc -v option to get a detailed report of all the build steps:

  $ gcc -v -o circle circle.c

STOP! 

Where to start?

The first step is, of course, to write the source code you want to compile. Yes you already have skill using a text editor (like emacs, vim, or any other),then you can jump to the next section.the next step is to write the program. We will use as example the iconic Hello world.








After writing the code, save the file somewhere. In this case, which is a C program, use the extension .c.

READY NOW...

Once we have our program written, it is time to compile it. For that, we need a console or terminal, which provides us with the command line interface.

Once open, use the cd command to move to the directory where

you saved your source code file. When you are in it, write the necessary command to compile it, which will depend on the programming language used:

• If we have written the program in C:



Example 1: compile the program hello.c, and generate the executable file a.out:


Example 2: compile the program hello.c, and generate the executable file hello:


Example 3: compile the program hello.cpp and generate the executable hello:


BRILLIANT!!


My program already compiles, what's next?

The time has come to run the program. If you wrote the source code in C or C ++,
all you have to do is write ./a.out (or whatever name you used with the -o option, like
./ hello in the example).








Comentarios