This blog is subject the DISCLAIMER below.

Friday, January 12, 2007

Advanced C++ part 2 : Necessary Introduction to the Compilation Process

Hello all of you out there :).
I know I am awfully late in putting this part, I apologize about that :$.
Anyway the issue was I have intended to put it in a book, I've already made the first chapter, but I found it too hard to make a whole book. So I've seen it should be better writing the series here then assembling it into a book.
Okay, let's start.
This article won't be so interesting, it just furnishes basics you need to understand how the program gets compiled; because that will make a difference in your understanding of why each feature was designed that way.
Our terminology here is :
source file
: the .cpp file.
header file: the .h file.
When using libraries, like cstdio, which has the printf function and cin and cout,
your program can't use them unless you include the cstdio header file (in C it was stdio.h, in C++ it is cstdio; yeah with no .h extension).
Unlike C# or Java, the C++ compiler never looks inside any other source file other than the one it is compiling right now. This means, it can't perfom any compile-time validations if you use an external function/class; as a matter of fact, it can't even produce a description of that function/class so the linker knows which function/class he wants (we will discuss linker later). This have many consequences. The first one is that this have introduced the need for header files. Header files gets included in the source file before the compiler processes it (#include is a preprocessor). So the compiler processes the preprocessed source file which includes the expanded header files; meaning the compiler doesn't understand what a header file means, it only knows what a source file means; also meaning if you #include <abc.tyr.eee.338.e8> it won't say anything. Also you can't compile a header file; it is only intended to be included in the source file.
So why don't we #include the other source files instead ? Well, there is a difference between a declaration and definition. The source file have definition, so if you included one source file inside the others, both will claim they have the code of that function, and the linker will not know which one have the right. The header file should have only declaration, which is declaring the type, not the implementation. So if two files includes the declaration, they only say that is the type we want to be linked to us, no collision because after all, some other file will be the only one to claim that he owns the implementation which will be then linked to the other files.
[NOTE: this is why a book is easier in some cases, you can add exmaples and pages as you like]

Next part will be about linkers. I think this is enough for one post.
IMPORTANT: I need feedback to know whether I should simplify more or any comment you want to say.

1 comment:

sos said...

thanks it was really useful.
i was searching for how to make my own libraries in devc++ and so i think to dig deep down the process .
both compilation and linking part was useful.
suggestion: you can blog about making libraries.