What happens when you type gcc main.c

Cristian Mendoza
3 min readJun 12, 2020

--

The main() serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program. A program usually stops executing at the end of main(), although it can terminate at other points in the program for a variety of reasons.

What is gcc ?

The original GNU C Compiler (GCC) is developed by Richard Stallman, the founder of the GNU Project. Richard Stallman founded the GNU project in 1984 to create a complete Unix-like operating system as free software, to promote freedom and cooperation among computer users and programmers.

GCC, formerly for “GNU C Compiler”, has grown over times to support many languages such as C (gcc), C++ (g++), Objective-C, Objective-C++, Java (gcj), Fortran (gfortran), Ada (gnat), Go (gccgo), OpenMP, Cilk Plus, and OpenAcc. It is now referred to as "GNU Compiler Collection".

What is the preprocessor ?

The preprocessor is a tool that helps us a lot when programming with C. It is part of the C Standard, just like the language, the compiler and the standard library.It parses our program and makes sure that the compiler gets all the things it needs before going on with the process.

What does it do, in practice?

For example, it looks up all the header files you include with the #include directive.It also looks at every constant you defined using #define and substitutes it with its actual value.

What is assambler ?

assembler is a program that translates assembly language (low-level language) source code into machine code (typically, in the form of object files). Assembly language is specific to the CPU architecture you’re targeting, so there are many different assembly languages. And there are often multiple dialects of assembly language for a specific CPU architecture.

A C compiler is a program that translates C (high-level language) source code into machine code (typically, in the form of object files). Some compilers provide an option to output assembly language source code, which can then be fed through an assembler to produce machine code.

What is the linker ?

The linker is a program that makes executable files. The linker resolves linkage issues, such as the use of symbols or identifiers which are defined in one translation unit and are needed from other translation units.In short, the linker’s job is to resolve references to undefined symbols by finding out which other object defines a symbol in question, and replacing placeholders with the symbol’s address. Of course, the process is more complicated than this; but the basic ideas apply.

once you have read all, we will able to understand the process of next examples.

Making the object file: the compiler

This tells the compiler to run the preprocessor on the file file.c and then compile it into the object code file file.o. The -c option means to compile the source code file into an object file but not to invoke the linker. If your entire program is in one source code file, you can instead do this:

gcc -c file.c -o file

The job of the linker is to link together a bunch of object files (.o files) into a binary executable. This includes both the object files that the compiler created from your source code files as well as object files that have been pre-compiled for you and collected into library files. These files have names which end in .a or .so, and you normally don't need to know about them, as the linker knows where most of them are located and will link them in automatically as needed.

References

  • Kernighan and Ritchie, The C Programming Language, 2nd Ed.
  • The man page for gcc. Type: man gcc at the unix prompt.

--

--

No responses yet