Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 16th, 2009, 7:43 AM   #1
magnus
Programmer
 
Join Date: Sep 2008
Posts: 95
Rep Power: 6 magnus is on a distinguished road
Header files & corresponding .cpp files

So, according to what I've learned, it's usually best to define functions and variables in the .h file, and have the implementation in the separate .cpp file, especially for larger headers. The first question is, is this a commonly accepted/"politically correct" way of doing it?

Second question, how do you link the files? Having "#include headername.cpp" at the bottom of the header makes sense to me, but I've seen "#include headername.h" in the actual cpp file as well, which doesn't make any sense to me... how does the compiler know where the functions are implemented, since it's usually the header file that's actually #included, but the header has no mention of the corresponding .cpp?

Also, lastly, any info on how big programs should be broken into files (common/good coding practices etc.) would be greatly appreciated.
magnus is offline   Reply With Quote
Old Oct 16th, 2009, 8:12 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 19 DaWei will become famous soon enoughDaWei will become famous soon enough
Re: Header files & corresponding .cpp files

The #include statement causes the included file to be copied and pasted into the including file. Perhaps that will help clear things up.

Header files serve the function of making declarations known to the compiler. A header file with widely used declarations might be included in several .cpp files. If actual definitions (implementations) were in the header file, then the linker would choke on multiple definitions. So don't put definitions in the header files. The compiler merely needs to know that any invocations in the source file are correctly stated. The declaration (prototype) is sufficient for that.

You may find definitions of class methods in a header file. That won't cause multiple definitions because the class isn't instantiated. The actual code won't be generated until it is (unless it's static).
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 16th, 2009, 4:47 PM   #3
magnus
Programmer
 
Join Date: Sep 2008
Posts: 95
Rep Power: 6 magnus is on a distinguished road
Re: Header files & corresponding .cpp files

Well, thanks for that, but that didn't really answer my question, as a matter of fact I already knew all of that... also,

Quote:
If actual definitions (implementations) were in the header file, then the linker would choke on multiple definitions. So don't put definitions in the header files.
Wrong, actually... that's what #include guards are for
magnus is offline   Reply With Quote
Old Oct 16th, 2009, 4:57 PM   #4
Benoit
Security Specialist
 
Benoit's Avatar
 
Join Date: Sep 2004
Posts: 1,053
Rep Power: 11 Benoit will become famous soon enough
Re: Header files & corresponding .cpp files

Quote:
Originally Posted by magnus View Post
So, according to what I've learned, it's usually best to define functions and variables in the .h file, and have the implementation in the separate .cpp file, especially for larger headers. The first question is, is this a commonly accepted/"politically correct" way of doing it?

Second question, how do you link the files? Having "#include headername.cpp" at the bottom of the header makes sense to me, but I've seen "#include headername.h" in the actual cpp file as well, which doesn't make any sense to me... how does the compiler know where the functions are implemented, since it's usually the header file that's actually #included, but the header has no mention of the corresponding .cpp?

Also, lastly, any info on how big programs should be broken into files (common/good coding practices etc.) would be greatly appreciated.
It doesn't have to know which file its in..That's the job of the linker after the modules have been compiled.
Benoit is offline   Reply With Quote
Old Oct 16th, 2009, 6:43 PM   #5
magnus
Programmer
 
Join Date: Sep 2008
Posts: 95
Rep Power: 6 magnus is on a distinguished road
Re: Header files & corresponding .cpp files

Quote:
Originally Posted by Benoit View Post
It doesn't have to know which file its in..That's the job of the linker after the modules have been compiled.
That doesn't make any sense to me... how does the linker know which files to "link" then?
magnus is offline   Reply With Quote
Old Oct 17th, 2009, 12:23 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 19 DaWei will become famous soon enoughDaWei will become famous soon enough
Re: Header files & corresponding .cpp files

I'm so glad to know you already knew the answers, and that I'm wrong. Actually, YOU'RE wrong about the purpose of header guards. Header guards are to keep an included file from being included more than once in one source file.

The compiler knows nothing about multiple source files. The compiler deals with one source file at a time. That one file may have a number of files included. Those inclusions may have declarations with no matching definition in the source file under compilation at the moment.

Multiple source files are compiled separately. There is no compiler visibility of these multiple files. The object files the compiler produces may very well have symbols which are unresolved insofar as where they will ultimately be located in the executable. Resolving those relationships is the province of the linker and possibly a locator.

An executable file a la Windows is probably a file that can be located at run time, what with memory mapping or tables of addresses that can be shifted at load time. On the other hand, it doesn't really have to be that way. Other systems need to have the addresses fixed prior to run time. Embedded systems with ROM would be one example.

I would respectfully suggest that you might engage the clutch to your brain before you pop the accelerator of your mouth. An alligator mouth can overload a hummingbird ass and never know until hellzapoppin and crapzaflyin.

The linker knows which files to link because you tell it which files to link. If you don't know how to do that, you can use an IDE which organizes your project and tells the linker for you.
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 17th, 2009, 7:03 AM   #7
magnus
Programmer
 
Join Date: Sep 2008
Posts: 95
Rep Power: 6 magnus is on a distinguished road
Re: Header files & corresponding .cpp files

Quote:
I'm so glad to know you already knew the answers, and that I'm wrong. Actually, YOU'RE wrong about the purpose of header guards. Header guards are to keep an included file from being included more than once in one source file.
I didn't know the answers, and I still don't. You didn't answer the question, but instead posted an irrelevant load of text - I know all of what you posted there, but as I said, it didn't address my question. As for the bolded part... I replied to this bit of your post:
Quote:
If actual definitions (implementations) were in the header file, then the linker would choke on multiple definitions. So don't put definitions in the header files.
The purpose of the include guards, as you said, is to "keep an included file from being included more than once in one source file", therefore preventing "choking on multiple definitions"(as you said) through multiple #includes of the same header, which is exactly why I addressed that bit of your post... you can well have implementations and everything in you header if you want to, because the #include guards prevent multiple ANYTHINGS that would result from multiple/circular #includes; what I would like to know is whether it's an accepted/"correct" practice

Anyways, even after all that wall of text, you still haven't answered my questions. Also,
Quote:
I would respectfully suggest that you might engage the clutch to your brain before you pop the accelerator of your mouth. An alligator mouth can overload a hummingbird ass and never know until hellzapoppin and crapzaflyin.
Thank you for that. I must also request that you engage the clutch of your brain as well, or perhaps be so kind as to replace it as it may be malfunctioning, as currently you're not answering my questions but are instead posting walls of text about memory mapping, linkers, locators and the likes, that are simply not answering my question, and seem for the most part irrelevant.

Quote:
The linker knows which files to link because you tell it which files to link. If you don't know how to do that, you can use an IDE which organizes your project and tells the linker for you.
Yea, but the thing that I was asking to begin with is what if the linker does not "know"?

Here, lemme illustrate:

-I have file a.h with class definitions and the likes
-I have a.cpp file with the implementation of the functions
-I can't see anything telling the linker to include the contents of the .cpp file
-I can see a line #include a.h in the .cpp file
-I'm not including the actual c.pp file in my program, I'm only just including the header file
-Now, how does the linker know where the implementation's supposed to be?
magnus is offline   Reply With Quote
Old Oct 17th, 2009, 7:12 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 19 DaWei will become famous soon enoughDaWei will become famous soon enough
Re: Header files & corresponding .cpp files

You maybe should get your hand fitted to a mop handle. In your example, you have one cpp file. It has, as content, the header file. You compile, with that source file as the argument. You link with that object file as the argument.

Should you have multiple source files, you compile each one independently. None knows anything about the others aside from declarations implicit information contained in the header files. You get a number of object files. You link with those object files as the arguments.

Quote:
you can well have implementations and everything in you header if you want to, because the #include guards prevent multiple ANYTHINGS that would result from multiple/circular #includes
This is just BS. The header guard will do nothing to prevent a definition for source file A from being repeated in source file B if both include the header. The linker will choke when it finds identical definition code in two of the object files.

Sheesh. Go sit behind the barn, watch the cotton grow, and think a bit. Hell, you could even TRY IT.
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 17th, 2009, 7:48 AM   #9
magnus
Programmer
 
Join Date: Sep 2008
Posts: 95
Rep Power: 6 magnus is on a distinguished road
Re: Header files & corresponding .cpp files

...well... okay than, I'm sorry if you're having a bad day, I didn't intend to be offensive or anything... but I'd much rather be helped without any derogatory comments, if possible. Not having knowledge or experience comparable to a more professional programmer doesn't make one suitable only for mopping and the likes.

Quote:
In your example, you have one cpp file. It has, as content, the header file. You compile, with that source file as the argument. You link with that object file as the argument.
I've explicitly specified which files to compile, I've always let the IDE handle that. However, at least with gcc+Fedora Eclipse, it only compiles the file with the main() function, as well as all headers included in that file.

Quote:
Should you have multiple source files, you compile each one independently. None knows anything about the others aside from declarations implicit information contained in the header files. You get a number of object files. You link with those object files as the arguments.
For the bolded part, as I said above, I haven't compiled each one independently... none of them are ever compiled unless explicitly included. Also, I've never done linking "by hand" (as in I've never had to actually invoke the linker to do it, I hit "build all" and that's all for me to do), hence my "confusion" on the topic.

Quote:
This is just BS. The header guard will do nothing to prevent a definition for source file A from being repeated in source file B if both include the header. The linker will choke when it finds identical definition code in two of the object files.
I'm not understanding you here... you said this:
Quote:
Header guards are to keep an included file from being included more than once in one source file.
...which I did know. Apparently you're talking about something slightly different... would you mind explaining?
magnus is offline   Reply With Quote
Old Oct 17th, 2009, 8:50 AM   #10
Benoit
Security Specialist
 
Benoit's Avatar
 
Join Date: Sep 2004
Posts: 1,053
Rep Power: 11 Benoit will become famous soon enough
Re: Header files & corresponding .cpp files

Quote:
Originally Posted by magnus View Post
...well... okay than, I'm sorry if you're having a bad day, I didn't intend to be offensive or anything... but I'd much rather be helped without any derogatory comments, if possible. Not having knowledge or experience comparable to a more professional programmer doesn't make one suitable only for mopping and the likes.

I've explicitly specified which files to compile, I've always let the IDE handle that. However, at least with gcc+Fedora Eclipse, it only compiles the file with the main() function, as well as all headers included in that file.

For the bolded part, as I said above, I haven't compiled each one independently... none of them are ever compiled unless explicitly included. Also, I've never done linking "by hand" (as in I've never had to actually invoke the linker to do it, I hit "build all" and that's all for me to do), hence my "confusion" on the topic.

I'm not understanding you here... you said this:
...which I did know. Apparently you're talking about something slightly different... would you mind explaining?

Use the gcc -c option, eg compile but do not link. If you don't do that, then it will look for a main() function, as it will not link without it.

I think you need to review the compile and link process.
Benoit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files and Implementation files simple question Soulstorm C++ 1 May 2nd, 2009 5:32 PM
Dev-C++ - #include .cpp/.h files Cache C++ 5 Nov 25th, 2005 11:34 AM
question about header files and prototyping functions linuxpimp20 C 13 Sep 7th, 2005 8:28 AM
Header files Klarre C++ 13 Jun 15th, 2005 11:25 AM
Including header files some1 C++ 8 Apr 28th, 2005 8:59 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC