![]() |
| ||
| Compilation process Hello, i am a naive in C/C++ and pretty confused about how does the compilation process work in the background, can you explain in detail (or give me a link about it) ? in particular can you please tell me: how are declarations in header file associated with the definitions in the .CPP file??Basically, I don't understand how a header file being included makes a connection with the source file that has the definitions for whatever is in the header file. It is my understanding I could name my header files something completely unrelated to the source files which contain the definitions and including the header files would still make the defined functions/whatnot usable. How? |
| ||
| Re: Compilation process |
| ||
| Re: Compilation process Quote:
#include "myheader.h" at the top of the cpp file. #include tells the pre-processor to bung the code in "myheader.h" right there.Yes you can call the header whatever you like, but why would you? it's not logical and programming is all about logic. Why are they separate? Because if you make flexible reuseable code (see my comments about being logical) it can be pre-compiled into a libray and used directly in other projects without having to be re-compiled for each project. The header file remains in its "plain text" state so the user of your library can see what methods are vailable and what they return etc. without having to skirt arround reams of code. the compiler process basically is: Pre-process (#include, #define, #ifndef macros etc...) Compile .cpp into .o object files link object files to an .exe or .dll blah... |
| ||
| Re: Compilation process The content of header file is prepended to source file. |
| ||
| Re: Compilation process >Basically, I don't understand how a header file being included makes a connection >with the source file that has the definitions for whatever is in the header file. It doesn't. There's no connection at all. The header is there to declare names so that you can compile your source. If the names aren't declared, you'll get compilation errors: #include <stdio.h>But if you declare the name, even if the object itself wasn't defined, the compiler won't complain: #include <stdio.h>When you try to build this code, the linker will start complaining because x was only declared, never defined. That's a different issue entirely. You can add another file that defines x and everything will work when you compile and link them together (no headers involved): /* File 1 */ /* File 2 */All a header does is provide declarations. It doesn't know about the linker, or any implementation files. It declares names and doesn't give a hooey about whether they're defined or not. That's the job of the linker. So, you can throw the extern int x; part into a header and call it good:/* Header file */ /* File 1 */ /* File 2 */The #include direction just replaces itself with the contents of the file you specify, so after preprocessing header.h, file 1 looks just like this (which is functionally identical to what we did before): /* File 1 */That's all there is to it. |
| ||
| Re: Compilation process Hmm ok...now i have understood it all.Thanks to all of you!! |
| All times are GMT -4. The time now is 6:49 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC