473,566 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What sense do h files make in c/cpp?

xz
What sense do h files make in c/cpp?
I never thought about this question before.

Does the existence of h files make sense at all?

Why do we need to declare functions in h files and define/implement
them in cpp?

Jul 2 '07 #1
9 2198
xz wrote:
What sense do h files make in c/cpp?
I never thought about this question before.

Does the existence of h files make sense at all?

Why do we need to declare functions in h files and define/implement
them in cpp?
When you write code you can either: 1) put everything into a single file
or, 2) separate your code into multiple files. Except for very small
projects, option 1 is not practical, possible, and/or desirable. One
enormous source file is difficult to maintain (not to mention,
comprehend), slow to compile, and often outright impossible, such as
when interfacing with third party code for which the source is not
publicly available.

C++ supports "separate compilation" which means that individual source
files can be compiled separately and the end result can be combined into
an executable. Invariably parts of one source file will make reference
to parts of another source file (e.g., calling a function or using a
class defined elsewhere). The header file typically contains the
minimum information necessary for the compiler to understand such a
reference to the contents of another source file.

-Mark
Jul 2 '07 #2
xz
Thank you for your reply.

But I was not asking about Single File vs Multiple Files.
I definitely understand the significance of separating code into
different files.

However, why don't we always put the code describing one object, i.e.
both the declaration and the definition of one class, in the same
file, say, the cpp file?
When using this class, you can simply include the corresponding cpp
file, instead of include the h file like what we are currently doing.

If the use of h file is only for the speed of compiling, I don't see
much significance.
It's not worth to make the structure of the files much more
complicated only to save a little compiling time.
On Jul 2, 3:57 pm, Mark P <use...@fall200 5REMOVE.fastmai lCAPS.fm>
wrote:
xz wrote:
What sense do h files make in c/cpp?
I never thought about this question before.
Does the existence of h files make sense at all?
Why do we need to declare functions in h files and define/implement
them in cpp?

When you write code you can either: 1) put everything into a single file
or, 2) separate your code into multiple files. Except for very small
projects, option 1 is not practical, possible, and/or desirable. One
enormous source file is difficult to maintain (not to mention,
comprehend), slow to compile, and often outright impossible, such as
when interfacing with third party code for which the source is not
publicly available.

C++ supports "separate compilation" which means that individual source
files can be compiled separately and the end result can be combined into
an executable. Invariably parts of one source file will make reference
to parts of another source file (e.g., calling a function or using a
class defined elsewhere). The header file typically contains the
minimum information necessary for the compiler to understand such a
reference to the contents of another source file.

-Mark

Jul 2 '07 #3
xz
Here is am example for what I am talking about:

Declare and define the Apple class in one single file "Apple.cpp" .
Then include this file in the "AppleTest. cpp" file to use it.

//Apple.cpp
#include<iostre am>

using namespace std;

class Apple{
public:
void print();
};

void Apple::print() {
cout<<"I am an apple"<<endl;
}

//AppleTest.cpp
#include "Apple.cpp"

using namespace std;

int main() {
Apple apple;
apple.print();
}


On Jul 2, 3:57 pm, Mark P <use...@fall200 5REMOVE.fastmai lCAPS.fm>
wrote:
xz wrote:
What sense do h files make in c/cpp?
I never thought about this question before.
Does the existence of h files make sense at all?
Why do we need to declare functions in h files and define/implement
them in cpp?

When you write code you can either: 1) put everything into a single file
or, 2) separate your code into multiple files. Except for very small
projects, option 1 is not practical, possible, and/or desirable. One
enormous source file is difficult to maintain (not to mention,
comprehend), slow to compile, and often outright impossible, such as
when interfacing with third party code for which the source is not
publicly available.

C++ supports "separate compilation" which means that individual source
files can be compiled separately and the end result can be combined into
an executable. Invariably parts of one source file will make reference
to parts of another source file (e.g., calling a function or using a
class defined elsewhere). The header file typically contains the
minimum information necessary for the compiler to understand such a
reference to the contents of another source file.

-Mark

Jul 2 '07 #4
On 2007-07-02 23:33, xz wrote:
On Jul 2, 3:57 pm, Mark P <use...@fall200 5REMOVE.fastmai lCAPS.fm>
wrote:
>xz wrote:
What sense do h files make in c/cpp?
I never thought about this question before.
Does the existence of h files make sense at all?
Why do we need to declare functions in h files and define/implement
them in cpp?

When you write code you can either: 1) put everything into a single file
or, 2) separate your code into multiple files. Except for very small
projects, option 1 is not practical, possible, and/or desirable. One
enormous source file is difficult to maintain (not to mention,
comprehend), slow to compile, and often outright impossible, such as
when interfacing with third party code for which the source is not
publicly available.

C++ supports "separate compilation" which means that individual source
files can be compiled separately and the end result can be combined into
an executable. Invariably parts of one source file will make reference
to parts of another source file (e.g., calling a function or using a
class defined elsewhere). The header file typically contains the
minimum information necessary for the compiler to understand such a
reference to the contents of another source file.

Thank you for your reply.
First off, please don't top-post, and don't quote signatures, thanks.
But I was not asking about Single File vs Multiple Files. I
definitely understand the significance of separating code into
different files.

However, why don't we always put the code describing one object, i.e.
both the declaration and the definition of one class, in the same
file, say, the cpp file?
When using this class, you can simply include the corresponding cpp
file, instead of include the h file like what we are currently
doing.
Because if you do it that way you'll have to compile that code each time
you include it in some other file. By separating it you only have to
compile it once. Imagine a large project with a .h/.cpp pair of files
describing a class using throughout the project, imagine that this class
is quite big, and that the project have a couple of hundred files. With
your solution you'd have to compile that large class a couple of hundred
times, as it is now you only have to do it once.
If the use of h file is only for the speed of compiling, I don't see
much significance.
It's not worth to make the structure of the files much more
complicated only to save a little compiling time.
For large projects it not just a little time, it might be quite
significant amounts of time. Remember that the most expensive thing in
software development is the people working on the project, each minute
they have to wait on the compiler is wasted money.

--
Erik Wikström
Jul 2 '07 #5
On Jul 2, 11:33 pm, xz <zhang.xi...@gm ail.comwrote:
But I was not asking about Single File vs Multiple Files.
I definitely understand the significance of separating code into
different files.
However, why don't we always put the code describing one object, i.e.
both the declaration and the definition of one class, in the same
file, say, the cpp file?
Because that's a bad policy. The two are generally the
responsibility of two different people. It's important to keep
them separate, and since files seem to be the organisational
method of choice...
When using this class, you can simply include the corresponding cpp
file, instead of include the h file like what we are currently doing.
Again, files seem to be the organisational method of choice:-).
In this case, the choice has been made by the build
system---all of the build systems I know are file based.
Including a .cpp file will mean recompiling all of the client
code any time you make a minor change in the implementation.
Definitly to be avoided.

And again, it would mean that you cannot acquire modification
rights on the implementation without also acquiring them on the
interface. Which is not a good thing.
If the use of h file is only for the speed of compiling, I don't see
much significance.
You don't see much significance between having to recompiler 20
or 30 lines, and having to recompile 2 million lines, just
because of trivial change in the implementation?
It's not worth to make the structure of the files much more
complicated only to save a little compiling time.
First, I think that the "complexity " is necessary for other
reasons anyway. But I don't consider a difference between 5 or
10 seconds and several hours just "a little".

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 3 '07 #6
On Jul 3, 2:49 pm, James Kanze <james.ka...@gm ail.comwrote:
On Jul 2, 11:33 pm, xz <zhang.xi...@gm ail.comwrote:
But I was not asking about Single File vs Multiple Files.
I definitely understand the significance of separating code into
different files.
However, why don't we always put the code describing one object, i.e.
both the declaration and the definition of one class, in the same
file, say, the cpp file?

Because that's a bad policy. The two are generally the
responsibility of two different people. It's important to keep
them separate, and since files seem to be the organisational
method of choice...
When using this class, you can simply include the corresponding cpp
file, instead of include the h file like what we are currently doing.

Again, files seem to be the organisational method of choice:-).
In this case, the choice has been made by the build
system---all of the build systems I know are file based.
Including a .cpp file will mean recompiling all of the client
code any time you make a minor change in the implementation.
Definitly to be avoided.

And again, it would mean that you cannot acquire modification
rights on the implementation without also acquiring them on the
interface. Which is not a good thing.
If the use of h file is only for the speed of compiling, I don't see
much significance.

You don't see much significance between having to recompiler 20
or 30 lines, and having to recompile 2 million lines, just
because of trivial change in the implementation?
It's not worth to make the structure of the files much more
complicated only to save a little compiling time.

First, I think that the "complexity " is necessary for other
reasons anyway. But I don't consider a difference between 5 or
10 seconds and several hours just "a little".

--
James Kanze (GABI Software) email:james.ka. ..@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
And also all the function definitions which are in the .cpp file are
compiled and put in their respective .o (object files). So if you
include the .cpp files the function definitions are then compiled
again with the .cpp which includes this .cpp file and then the linker
will say " there are multiple definitions of the same function" in
file_included.o and file_which_incl udes.o
The linker may either complain and stop or it may link with the first
symbol it found, which incidently is correct in this case and doesn't
absolve you of the sin.

Hope That Helps.
--
Regards,
Taran

Jul 3 '07 #7
xz wrote:
What sense do h files make in c/cpp?
1) You often want to create functions, types and constants which
are local to one compilation unit and which you don't want
interfering with the rest of the program. For example, you might
want to have something like this in a cpp file:

namespace { const int LOOPS = 42; }

and you want that to be local to the current cpp file and not seen
anywhere else. You don't need to worry if some other cpp also uses
the name "LOOPS" for something completely different.

2) Related to 1, but more generally: By exposing the entire cpp file
to other cpp files, you are breaking the principle of minimal public
interfaces. A header file should contain as little code as possible:
That is, the public interface of that module and the bare minimum
implementation details enforced by technical reasons of the compiler.
All the implementation details which are not necessary in the header
file for technical reasons should be "hidden" in the cpp file. This
increases abstraction and modularity, which is good in the long run.

3) For technical reasons, if you are going to include a cpp file in
more than one other cpp file, all the functions in that cpp file must
be 'inline' or else you will get linker errors because of duplicate
implementations .

4) Related to that: If you have a static member variable or a static
variable local to that cpp file (usually inside a nameless namespace
or such), you *will* get a linker error if you include the cpp file in
more than one other cpp file. That's because the linker will see two
instances of that static variable and has no way of knowing which one
it should use. You can't "inline" static variables as you can do with
functions.

5) Compiling each cpp file separately (and keeping all headers as
minimal as possible and with as few #includes as possible) will
greatly speed compilation times in large projects. Usually if you
just modify one cpp file then the compiler just has to recompile
that cpp file and then it can link everything into the final binary.
If, however, that cpp file was included in dozens of other cpp files,
which in turn are included in dozens of others and so on, modifying
one cpp file may suddenly require for the compiler to recompile most
of the others too, for no good reason. With very large projects that
could take hours instead of a few seconds.

6) Likewise, compiling one cpp file would take much longer if it
includes dozens of other cpp files (which themselves include dozens
of others and so on).

7) If module A references module B, and module B references module A,
you have a problem. With minimal headers this problem can usually
be solved (as long at least one of A or B uses only references/pointers
to the other). However, if you always included the cpp files, you would
have no solution to this problem.

8) Header/source file pairs is not the only possible way of compiling.
Bunch-of-headers/big-precompiled-library is another very common
situation (which is actually almost always the case with the standard
libraries of the compiler). There's no reason why you would want to
have the entire source code of the library when just the headers are
enough to use it.
Jul 3 '07 #8
xz

Sorry for top-posting and thanks for reminding. I am a rookie here
that I did not know much about those things.

Also, thank you guys a lot for providing so many details.

I am actually switching from Java to C++. And in Java, I don't have
this header file thing. That's why I had the question.

Now, my understanding is that h files work as an interface between the
developers of the modules (the corresponding cpp files or the .o
files) and the users of the modules, i.e. the clients.
So they are like manuals and control panels. Basically, you read the h
files to know the usage of the classes and functions, and use them by
dealing with the h files.
Is that correct?

Jul 3 '07 #9
xz wrote:
I am actually switching from Java to C++. And in Java, I don't have
this header file thing.
That is, of course, a major defect of Java. Not that it doesn't
have header files, per se, of course---the C++ solution of
textual inclusion is far from elegant---but that it doesn't have
any means whatsoever of separating the external specification
from the actual code. That makes Java significantly more
difficult to use in larger projects.
That's why I had the question.
Now, my understanding is that h files work as an interface between the
developers of the modules (the corresponding cpp files or the .o
files) and the users of the modules, i.e. the clients.
So they are like manuals and control panels. Basically, you read the h
files to know the usage of the classes and functions, and use them by
dealing with the h files.
Is that correct?
More or less. I'm not sure about "reading the .h files"; on
most of the larger projects I've been on, they've been
automatically generated from the specifications (using Rational
Rose, or something of that sort), and aren't necessarily very
readable. But the .h files do represent a different type of
information, generally created at a different level. (This
isn't totally true, since they have to specify the private
members of the class as well. But with rigorous use of the
compilation firewall idiom...)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
6385
by: Tony Marston | last post by:
Several months ago I started a thread with the title "What is/is not considered to be good OO programming" which started a long and interesting discussion. I have condensed the arguments into a single article which can be viewed at http://www.tonymarston.net/php-mysql/good-bad-oop.html I fully expect this to be the start of another flame...
54
6524
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature...
125
14594
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
121
9969
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather...
16
2650
by: thenightfly | last post by:
Ok, I know all about how binary numbers translate into text characters. My question is what exactly IS a text character? Is it a bitmap?
7
5974
by: MrNobody | last post by:
I was a Java developer so I'm used to using property files as a means to keep configuration settings for my apps. I'm wondering what options are there with ..NET? Some settings I want to include are like a root directory for a target program on the user's machine my app uses, which they would be prompted to supply at startup. Normally the...
1
2151
by: mikeotp | last post by:
Does the statement make sense? Meaning of “program†is to setup *.h(header檔) and to edit a main file to be a program exit. The main file includes all of the“*.h†files I edited before. thank you any positive suggestion is welcome
669
25640
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990....
3
1150
by: Developer.Man4 | last post by:
Hi all, i've been investigating for quite a while for files that are mandatory to be deployed to the server for an asp.net v1.1 application to run, to make it more clear, we don't need to move the source code files, .vb and .cs we have their assemblies deployed on the server. i also wanted to find out if we can change default locations for...
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.