DEV Community

Pierre Bouillon
Pierre Bouillon

Posted on • Updated on

Explain me C compilation like I'm five

With/Without librairies, makefiles, several files, etc.
I'm a truly beginner with this and I didn't understand a damn thing.

Thanks !

Top comments (6)

Collapse
 
lpasqualis profile image
Lorenzo Pasqualis

Your computer doesn't understand C. Your computer understands machine codes, which run directly on the CPU. A compiler is a program that transforms C code into machine code so that your computer can run it.

A C program can be all in one C file, or in many. It doesn't really matter how it is organized, it is still a program. This is similar to writing a book. It doesn't matter if you put the entire book in a single Word file, or if you split each chapter into a separate Word file. The book is still the book, the organization in one or many files is a choice you make to help you manage the work. C programs are the same. You can split a program into many files, or put it all in one file. When the program gets compiled, it all ends up in a single machine code binary that can be executed.

Libraries are simply pieces of compiled or non-compiled C code that you or somebody else wrote to do common things, and that you can use to write your program. The compiler takes them and compiles them into your program together with all the rest of the code.

A Makefile is like a recipe that tells Make what to do. Make is a utility to run the C compiler, telling it where to find the various files, libraries, etc. So... imagine Make as a cook and a Makefile as a recipe. The ingredients are the C files and libraries, and the resulting dish is an executable program.

Hope this helps.

Collapse
 
mcr42_19 profile image
mcr42 • Edited

To dig a bit deeper, the process of building a program consists actually of 2 parts:
Compiling is converting each C file into machine code ( objects), and
Linking is putting all the objects together to create the executable file.
Libraries are (mostly) just prebuilt object files someone else prepared for you.

Collapse
 
andreanidouglas profile image
Douglas R Andreani

What is the role of the OS here? Does it interfere with the type of machine code? Does the compiler require to link additional libraries in order to run the program on certain os?

Collapse
 
lpasqualis profile image
Lorenzo Pasqualis

When you compile a program you might have to choose the target system.

Usually, a compiler that runs on a system will compile for that system, but that is not always the case. (For example, you can compile an Arduino sketch on a Mac. The compiler runs on the Mac, but the executable runs on the Arduino.)

Depending on the program, the compiler will most likely have to link your code with OS specific libraries. Some libraries are standard, and always available on all OS's (example: standard IO in C). Using only standard libraries make your code portable to many systems.

Other libraries are not standard and make your code not easily portable to other OSs.

Collapse
 
pbouillon profile image
Pierre Bouillon

Thanks ! Very helpful !

Collapse
 
brovic profile image
Victor Ordu

Lovely.