Still makes zero sense that LORD VADER lost to Ewan Mcgregor

You down with gcc ? (Yeah, you know me)

The story of the command gcc main.c

Are you familiar with the term compilation? Well, if not — it is defined as “the action or process of producing something, especially a list, book, or report, by assembling information collected from other sources”

This is important because of what the letters gcc stand for and what the command itself does. When you code (write) a file in the C programming language, it must be saved with the file extension .c so the computer knows what type of file it is and therefore which program it should use to open and work with, said file. On another use for file extensions is to inform and/or remind the user what type of file they are looking at.

This is An-akin(see what I did there?) to the file extension.jpg for images, or.doc for your Microsoft Word documents.

So, back to compilation. When writing code in the C programming language, the purpose is to eventually execute the instructions you are giving the computer. Therefore the file must be made executable somehow. However, the machine does not understand the C language, at least not in the human readable form it is written in. Basically, the .c file is the source code, which consists of all the function definitions to be performed by the program. The computer only understands binary code — 0’s and 1’s — that’s it.

Therefore, we need a means of translating our C code from words to 0’s and 1’s. Enter gcc. To understand what gcc means, first we must understand what a compiler is and what cc means. A compiler is “a program that converts instructions into machine-code or lower-level form so that they can be read and executed by a computer”. With that in mind, cc means — C language compiler. The final step then, is to add the gand therefore understand the entire gcc command. The g in gcc stands for GNU — which means “GNU’s not Unix”, it is an operating system (the software that supports a computer’s basic functions, such as scheduling tasks, executing applications, and controlling peripherals), much like Unix is. GNU is a completely free software system, upward compatible with Unix. So, finally gcc is the command which calls — the GNU Compiler Collection — which is a compiler system produced by the GNU Project supporting various programming languages (including C).

This is a gnu (large type of antelope) not to be confused with GNU

With all of that in mind and without further ado — Here is the bread and butter of this blog post. Here is the process behind gcc main.c

main.c example file
#include <stdio.h>
/**
* main - entry point of program
*
* Return: 0 - always - success
*/
int main(void) 
{
printf("Hello Holberton!");
return (0);
}

So, in order to get this program to display it’s output we must make it executable by compiling it.

The process can be, and is, split into four separate stages: Preprocessing, compilation, assembly, and linking.

preprocessing

The first stage of compilation is called preprocessing. During this stage, the preprocessor program removes lines in your program, that begin with a # . This character is interpreted by the preprocessor as being indicative of preprocessor commands. This serves to reduce repetition in source code by providing functionality to inline files and to conditionally omit code.

Before interpreting commands, the preprocessor does some initial processing, such as removing comments from your file.

If you wanted to pause the compiler after this first stage, and display the result, you would pass the -E option to gcc:

gcc -E main.c

Given the main.c example above, the preprocessor will produce the contents of the stdio.h header file and combine it with the contents of themain.c file, with the comments deleted.

compiling

During this stage, the code from the preprocessor is translated to assembly code, which is specific to the specific computer’s processor. The existence of this step allows for C code to contain inline assembly instructions and to allow for the use of different assemblers, in the third stage of compilation (coming up next).

If you wanted to pause the compiler after this first stage, and display the result, you would pass the -E option to gcc:

gcc -S main.c

This will create a file named main.s, containing the generated assembly instructions.

assembly

During this third stage of the process, an assembler program is used to translate the assembly instructions to machine code, also known as object code. The output consists of actual, low level instructions to be run by the processor. This is the binary code you may be familiar with — 0’s and 1's.

gcc -c main.c

Running the above command will create a file named main.o, containing the object code of the program. As we learned during today’s lecture at Holberton School, the contents of such a file can be inspected using hexdump. This produces a

linking

In the final stage of the compiling process, the object code generated in the assembly stage (machine instructions — binary code) is used to put the finishing touches on producing an executable program. However, sometimes things are not quite in the proper order or the program may not have all it needs to carry out its function(s). To produce an executable program, the existing pieces have to be re ordered and anything missing needs to be included.

The linker program basically works with the object code so that functions can successfully call and reference other functions that they need to interact with and/or are dependent upon and vice versa. It also adds the instructions for library functions used by the program.

The culmination of this final stage of the compiling process is a fully operational, executable file. When run without options, gcc will name this file a.out. To save the file as something more useful and/or descriptive than the aforementioned a.out — we need to run the following command :

gcc -o main.c main

And that’s all she wrote. Join us next time for fun, fun, fun (till her daddy takes the T-bird away).