Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 01-31-2005, 11:08 PM   #1 (permalink)
Rafkin
Registered User
 
Rafkin's Avatar
 
Join Date: Mar 2004
Posts: 20
Rafkin is on a distinguished road
Including Header Files and their cpp counterparts

Something I keep seeing, in tutorials, and even on here, is this.

When you make a seperate file for code, such as a class to use in a file, most examples go like this:

someclass.h - This is where you define the class and prototype the functions
someclass.cpp - This is where you define the functions of the class
main.cpp - where the main program runs from.

Inside the someclass.h you do the prototyping and class defining, no includes are needed beyond normal ones, such as <iostream> etc..

Inside the someclass.cpp you include the someclass.h then do the definitions of the class functions etc.

Inside the main.cpp file you include the someclass.h but do NOT include the someclass.cpp. (this is the part that is confusing me.)

If i understand correctly, the compiler compiles the code in the file, and when it finds an include, it jumps to that file, then returns to the main file. Most tutorials include the header file, but not the cpp file. This suggests to me that while the class get's defined, and it's functions prototyped, the cpp file with the function definitions never gets included.

So how exactly does the compiler work? does it automatically look for a header's cpp counterpart? Or do you need to compile those into obj's seperately and link them into the executable?

I'm using the borland free command line tools, wich is handy, but unless i learn makefiles, i don't see how to compile those extra cpp files into objects and link them in..

Anyone able to clear this up for me?

---------------------
edit::

I just tested it, and found that it would give me an error just including the header file. I changed it to include only the cpp file (which in turn included the header file) and it worked fine.

While this clears up how the compiler works, and it is what i expected, this still confuses me in that almost all tutorials say to include the header, but not the cpp counterpart. Perhaps these are to be compiled seperately and linked in? If so, how would that work?

Again, i'm using the borland free command line tools with the Free Crimson Editor IDE.. is there a way to setup the crimson editor to do this? perhaps someone could help me figgure out how make files work? Maybe the make files are what I need. Or is there some other way?

Last edited by Rafkin; 01-31-2005 at 11:48 PM.
Rafkin is offline   Reply With Quote
Old 02-01-2005, 02:20 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
If i understand correctly, the compiler compiles the code in the file, and when it finds an include, it jumps to that file, ...
Nope.
The pre-process adds all the needed files based upont the pre-processor directives (#include ...). It merges that code with the main entry point.
Then the compiler makes it to obj files.
Then the linker mixes the obj files with libraries to create an executable.
__________________
Valmont is offline   Reply With Quote
Old 02-03-2005, 12:51 PM   #3 (permalink)
Rafkin
Registered User
 
Rafkin's Avatar
 
Join Date: Mar 2004
Posts: 20
Rafkin is on a distinguished road
Ok, so i include the header file for myclass, so that the main program has at least the definition of the class, then i include it also into the myclass.cpp so that when that object gets compiled, it has the definition as well. To prevent the class from being defined twice, I use

#ifndef _myclass_h_
#define _myclass_h_
// put in class definition
#endif

only, if the myclass.cpp and the main.cpp are both getting compiled seperately, before being linked togeather into the executable, then wouldn't the _myclass_h_ be undefined for each, and thus end up defining the class twice?
Rafkin is offline   Reply With Quote
Old 02-06-2005, 04:03 PM   #4 (permalink)
Rafkin
Registered User
 
Rafkin's Avatar
 
Join Date: Mar 2004
Posts: 20
Rafkin is on a distinguished road
Ok, I managed to figgure out enough about make files to know how to use them.. or at least, in a basic sense.. Although, i'm finding, that if I don't use the #ifndef and #endif on my sample program, it had no problem.

Though one of the tutorials i read says we should use them in the header files, like such:

#ifndef _myclass_h_
#define _myclass_h_
// .. stuff here
#endif

so I must ask, when is an #ifndef important and when is it just bloat?
Rafkin is offline   Reply With Quote
Old 02-06-2005, 08:58 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Include it always in headers. No matter what:
- If you won't re-use won't multiple include your library then others might.
I can't say why you have this error in your code because I've never seen it.
__________________
Valmont is offline   Reply With Quote
Old 02-06-2005, 10:48 PM   #6 (permalink)
Rafkin
Registered User
 
Rafkin's Avatar
 
Join Date: Mar 2004
Posts: 20
Rafkin is on a distinguished road
well, not doing libraries in this case.. and i finally got a chance to talk to some ppl and figgured out where i'm getting confused..

I was getting implementation and interface mixed up.. I was thinking that the linker sort of re-compiled the obj files into one exe file.. but as it was explained to me, the exe is sort of a zip file of obj files..

As such, the #ifndef doesn't even matter by the time it gets to the linker.

I think i'm straightened out now.. i hope.
Rafkin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:44 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting