How to compile, assemble and create object files from .c file? (C source code?)

Some information I would like to remember in the future, content summary of: Head First C.

Let's start with basics, and let's create a header file called koko.h:
int kokomoko(int x);
Ok, that was not hard. Lets implement this file in koko.c:
int kokomoko(int x){
 return x+1;
}
Ok now we have some piece of code that is not capable of running by itself, but it can be used by some other code. Lets demonstrate this. Create this file hello.c:
#include <stdio.h>
#include "koko.h"
 
int main(){
 int y = 10;
 int x;
 x = kokomoko(y);
 printf("%i",x);
 puts("");
}
Ok first try:
When you write the name of the header file, make sure you surround it with double quotes rather than angle brackets. When the compiler sees an include line with angle brackets, it assumes it will find the header file somewhere off in the directories where the library code lives. But your header file is in the same directory as your .c file. By wrapping the header filename in quotes, you are telling the compiler to look for a local file.

Compilation behind the scenes

Preprocessing: Fixing The Source Code
The first thing the compiler needs to do is fix the source. It needs to add in any extra header files its been told about using the #include directive. It might also need to expand or skip over some sections of the program.

Compilation: Translate The Code To Assembly
The step to generate machine code; which is converting the C source code into assembly language symbols.

Assembly: Generate The Object Code.
The compiler will need to assemble the symbol codes into machine or object code. This is the actual binary code that will be executed by the circuits inside the CPU.

    10010101 00100101 11010101 01011100

If you give the computer several files to compile for a program, the compiler will generate a piece of object code for each source file. But in order for these separate object files to form a single executable program, one more thing has to occur:

Linking: Put Everything Together
Once you have all of the separate pieces of object code, you need to fit them together to form the executable program. The compiler will connect the code in one piece of object code that calls a function in another piece of object code. Linking will also make sure that the program is able to call library code properly. Finally, the program will be written out into the executable program file using a format that is supported by the operating system. The file format is important, because it will allow the operating system to load the program into memory and make it run.

Back to our example:
Having koko.h inside the main program will mean the compiler will know enough about the kokomoko() function to compile the code. At the linking stage, the compiler will be able to connect the call to kokomoko(x) in koko.c.

Bullet Points So Far:
You can share code by putting it into a separate C file.
You need to put the function declarations in a separate .h header file.
Include the header file in every C file that needs to use the shared code.
List all of the C files needed in the compiler command.

Object Files

If the source code does not change, the object code does not change either. We do not need the first 3 steps for every file. We only need it for changed files. Then we can link the most recent files.

Save Copies of the Compiled Code
If you tell the compiler to save the object code it generates into a file, it shouldn't need to recreate it unless the source code changes. If a file does change, you can recreate the object code for that one file and then pass the whole set of object files to the compiler so they can be linked.



If you change a single file, you will have to recreate the object code file from it, but you won’t need to create the object code for any other file. Then you can pass all the object code files to the linker and create a new version of the program.

You want object code for each of the source files, and you can do that by typing this command:
gcc -c *.c
gcc -c will compile the code but won’t link it.

The *.c will match every C file in the current directory, and the -c will tell the compiler that you want to create an object file for each source file, but you don’t want to link them together into a full executable program.

Then, link them together
Now that you have a set of object files, you can link them together with a simple compile command. But instead of giving the compiler the names of the C source files, you tell it the names of the object files:
gcc *.o -o launch
The compiler is smart enough to recognize the files as object files, rather than source files, so it will skip most of the compilation steps and just link them together into an executable program called launch.

Let's try all this:
If you change just one of the files, you’ll only need to recompile that single file and then relink the program.
gcc -c koko.c 
gcc *.o -o launch
Very good..