What are header files used for?

I don't get how and when you use header files. I've always been programming with everything in one big '.cpp' file. I'd be grateful if somebody would explain this to me.
Look at this program:
1
2
3
4
5
6
#include <iostream>
int main()
{
    std::cout << "Hello World";
    return 0;
}

iostream haven't the extension .h but it is a header file

Headers have the declarations for symbols compiled separately (other cpp files or libraries ) so you can use them.
an #include "something.h" is found, that line is replaced by the contents of something.h

In the example above iostream is used for std::cout




Example:
1
2
//MyFunc.h
void MyFunction(int);

1
2
3
4
5
//MyFunc.cpp
void MyFunction(int n)
{
     // do something
}

1
2
3
4
5
6
7
//main.cpp
#include "MyFunc.h"

int main()
{
     MyFunction(5);
}

In the main file MyFunction is called but without including the header it wouldn't have been recognised
Last edited on
But why is it necessary to keep a function declaration separate from its definition, in two separate files?
If you do something like this:
1
2
3
4
5
//MyFunc.h
void MyFunction(int n)
{
     // do something
}

and include it in multiple files liked together you will get Linker errors as the same symbol is defined multiple times in multiple files
Header files serve several purposes.

1.) Break up and organize the code. When you're writing programs that are only a couple hundred to couple thousand lines of code, this isn't necessary. When you're working on a project that is several hundred thousand lines of code, it's absolutely essential.

2.) If properly organized, recompiling large projects doesn't require recompiling the entire project, only the code that has changed since the last compile. Again, with small programs that compile in less than a minute, this isn't necessary.

3.) When working with a team of programmers on a single large project, each member will be working on a seperate component, contained in a separate subset of files. If everything was contained in one large file, having multiple people working on that one file at the same time just adds another layer of impracticality to the scenario.

Bottom line, header files are for bigger projects. No need to worry about them till your projects start grouping to several thousand lines of code and larger. At that point, it will be useful to learn just how to properly organize your files to take advantage of their potential benefits. (for example, a poorly organized project might still require the bulk of the code get recompiled for just a small change in one file)
Header files are used also in small projects as is almost impossible creating a program without using any library. When using a library headers are required if you want to have the right declarations for that library.
Header files declare the interface to your classes, allow the separation of implementation, and serve as a quick reference to using your class.

Many times there is only 1 way to learn how to use a poorly documented class or library--the header files are the place to start figuring things out for yourself.

Or, you could implement some state-of-the-art, brand-new, useful, revolutionary library, coded privately; compile it, and distribute the binaries and header files for public use and never distribute your top-secret implementation code! It's like selling award-winning BBQ and not giving out the recipe. Mmmmmbbqq!
Last edited on
I tried it out but I keep getting errors:

1
2
3
4
5
6
7
8
// test.cpp

#include "a.h"
int main()
{
    writesomething("hello");
    return 0;
}


1
2
3
4
// a.h

#include <iostream>
void writesomething(char*);


1
2
3
4
5
6
7
8
// a.cpp

using namespace std;

void writesomething(char *str)
{
    cout << str;
}


When I compile, I get this error:
$ g++ ./test.cpp -o ./test.exe
./test.cpp: In function ‘int main()’:
./test.cpp:5: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccOxaPgp.o: In function `main':
test.cpp:(.text+0x76): undefined reference to `writesomething(char*)'
collect2: ld returned 1 exit status


And If I include 'a.cpp' as well:
$ g++ ./test.cpp ./a.cpp -o ./test.exe
./test.cpp: In function ‘int main()’:
./test.cpp:5: warning: deprecated conversion from string constant to ‘char*’
./a.cpp: In function ‘void writesomething(char*)’:
./a.cpp:5: error: ‘cout’ was not declared in this scope
Last edited on
a.cpp has to include a.h.
Oh OK, that solved it! Thanks for all your help!
Topic archived. No new replies allowed.