What happens when you type GCC main.c

What happens when you type GCC main.c

Lets start from the beggining

What is GNU?

In 1983 Richard Stallman announced the GNU Linux project (pronounced: GNOO LIN-?ks), which was developed as a derivative of the UNIX operating system. The project gained so much popularity that Richard and others founded the Free Software Foundation in 1985. Richard has been very outspoken on some of the early development of GNU and Linux. Research shows that the OS known as Linux is based on the Linux kernel but all other components are GNU based. Many software enthusiast believe that for Linus Torvalds to name Linux.. well.. Linux was unethical and that it should have been named GNU/Linux or GNU Linux to give credit to Richard’s contributions.

What is C?

C is an imperative procedural language. It was designed to be compiled using a relatively straightforward compiler to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support. Despite its low-level capabilities, the language was designed to encourage cross-platform programming. A standards-compliant C program written with portability in mind can be compiled for a wide variety of computer platforms and operating systems with few changes to its source code. The language is available on various platforms, from embedded microcontrollers to supercomputers.

C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to make utilities running on Unix. Later, it was applied to re-implementing the kernel of the Unix operating system.[6] During the 1980s, C gradually gained popularity. It has become one of the most widely used programming languages,[7][8] with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the ANSI since 1989 (see ANSI C) and by the International Organization for Standardization.

Basic C program

What is gcc?

The abbreviation gcc stands for GNU Compiler Collection and was originally written to be the compiler for the GNU OS. It contains compilers for the following languages:

C, C++, Objective-C, Fortran, Java, and more.

The gcc compiler was written in C and accepts options and file names as arguments so that engineers can have absolute control over the compilation process.

No hay texto alternativo para esta imagen

First the Preprocessor

When we run gcc the program (main.c for this example), gets processed by preprocessor. There are three things that preprocessor does:

  • First: it removes all the comments from our program main.c
  • Second: it includes code from header and source file
  • Third: it replaces macros (if there are any that are used in the program) with code.

Second . Compiler

The code generated by preprocessor is then given to compiler and compiler generates an assembly code from it.

Third. Assembly

The assembly code is converted to a binary code for the machine can interpret

And last but not least Linking

the complier puts all the libraries into de binary code and generate an executable file, we can give a name for that executable file with the command -o

There is others commands so we can stop the process in any step. they are:

  • -E which runs a C file through the preprocessor and saves the result into another file (in example below it’s 1_preprocessor)
  • -S that generates the assembly code of a C code and saves it in an output file (by default its the same name of the file but with extension .s)
  • -c that compiles a C file but does not link and saves it in an output file(by default its the same name of the file but with extension .o, also called object file)


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

Oscar Andres Montes Cuevas的更多文章

社区洞察

其他会员也浏览了