markdown guide
 

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.

 

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.

 

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?

 

What is the role of the OS here?

One of the roles of the OS is to handle memory management. It's the OS' job to manage pages of memory, and allow the program to read and write its own allocated memory while not allowing it to read memory outside of this space, for security reasons (This is why, if you're ever seen a joke about it, you can't dereference a null pointer referring to offset 0x0, because the OS won't let you access that protected space).

 

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.

 
 
Classic DEV Post from Apr 1

How I Write Online Articles

How you can write online articles

Pierre Bouillon profile image
Developer, student, tech enthusiast and coffee junky. I would be glad to speak with you!