If pre-processor copies the code from library to my source code, and then compiles the combined code, that should mean, that with my code, the library is also compiled, and linked. What does the linker do then?
#include pre-processor directive does not copy the code from libraries to your source code. It deals with header files (i.e., the ones that end with the
In the pre-processing phase, different preprocessor directives are resolved. For example, the
#include preprocessor directive, copies the contents of a header file and places them where it appears in your source code file. Learn more about header files!
To stop after preprocessing, you may use this command
gcc -E prog.c
Then comes the compilation phase in which your source code is translated into assembly language.
To stop after compilation, you may use this command
gcc -S prog.c
The assembling phase then, using an assembler, translates your program's source code into machine code (aka object code or machine language).
To stop after assembling, you may use this command
gcc -c prog.c
And lastly comes the linking phase in which the machine code of the libraries is merged with the machine code of your program using the linker to get the final executable file via which you can execute the program. We link external libraries to our programs with the
-l option when compiling.
Well, this is a really good question that I also( at a point of time) googled up and found this image, which is enough to explain everything
So, you can see in this image that the code is first pre processed and saved as a
filename.i file and then it's compiled where, its converted to an
filename.s, so, you must be thinking that where's my answer to the question, so, here's the thing, when this hello.i file is made, the C preprocessor checks back into that file and checks it for output(ie. see which functions your'e using in your code) and store them as well, in the new file it created which is filename.i, this filename.i is then sent to the C compiler, which performs the function of implementing the functions in the preproccesor, and for, which it has to read back into the same file to implement the function, hence, you have to do the job of mentioning the file twice!
Also, for reference, if your'e getting lost in this somewhere, see this table:
This table also includes some C++ stuff that I don't think you'll ever need, but it is there( so may ignore it as well). Also, if you are unable to understand what I'm talking of, read the 2nd chapter of the book, "Hacking:The Art Of Exploitation" by Jon Erickson as that explains all this stuff very deeply and to a huge extent, even in Assembly Code.