Compilation process

Reply

Join Date: Aug 2007
Posts: 18
Reputation: Ashu@sym is an unknown quantity at this point 
Solved Threads: 0
Ashu@sym Ashu@sym is offline Offline
Newbie Poster

Compilation process

 
0
  #1
Oct 11th, 2007
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?
Last edited by Ashu@sym; Oct 11th, 2007 at 10:02 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Compilation process

 
0
  #2
Oct 11th, 2007
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Compilation process

 
0
  #3
Oct 11th, 2007
how are declarations in header file associated with the definitions in the .CPP file??
Because you #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...
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,780
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 113
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Compilation process

 
0
  #4
Oct 11th, 2007
The content of header file is prepended to source file.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Compilation process

 
0
  #5
Oct 11th, 2007
>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:
  1. #include <stdio.h>
  2.  
  3. int main ( void )
  4. {
  5. x = 123; /* This line won't compile */
  6.  
  7. printf ( "%d\n", x );
  8.  
  9. return 0;
  10. }
But if you declare the name, even if the object itself wasn't defined, the compiler won't complain:
  1. #include <stdio.h>
  2.  
  3. extern int x; /* Declared but not defined */
  4.  
  5. int main ( void )
  6. {
  7. x = 123; /* Now this line compiles */
  8.  
  9. printf ( "%d\n", x );
  10.  
  11. return 0;
  12. }
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):
  1. /* File 1 */
  2. #include <stdio.h>
  3.  
  4. extern int x; /* Declared but not defined */
  5.  
  6. int main ( void )
  7. {
  8. x = 123; /* Now this line compiles */
  9.  
  10. printf ( "%d\n", x );
  11.  
  12. return 0;
  13. }
  1. /* File 2 */
  2. int x;
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:
  1. /* Header file */
  2. extern int x; /* Declared but not defined */
  1. /* File 1 */
  2. #include <stdio.h>
  3. #include "header.h"
  4.  
  5. int main ( void )
  6. {
  7. x = 123; /* Now this line compiles */
  8.  
  9. printf ( "%d\n", x );
  10.  
  11. return 0;
  12. }
  1. /* File 2 */
  2. int x;
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):
  1. /* File 1 */
  2. #include <stdio.h>
  3. /* Header file */
  4. extern int x; /* Declared but not defined */
  5.  
  6. int main ( void )
  7. {
  8. x = 123; /* Now this line compiles */
  9.  
  10. printf ( "%d\n", x );
  11.  
  12. return 0;
  13. }
That's all there is to it.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 18
Reputation: Ashu@sym is an unknown quantity at this point 
Solved Threads: 0
Ashu@sym Ashu@sym is offline Offline
Newbie Poster

Re: Compilation process

 
0
  #6
Oct 11th, 2007
Hmm ok...now i have understood it all.Thanks to all of you!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC