Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.

Join our community!

If you are already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Header files; How to use them
Topic Started: Jul 19 2008, 10:24 AM (649 Views)
Ruud v A
Member Avatar
Programmer · Artist
[ *  *  *  *  * ]
So you've finally written your first C++ program, but the code became longer than 5 pages... Then you started wondering "Can't I split this up in multiple files?" But then a problem occurred. How can you call a function from one file in an other file? The answer is header files. But header files are usable for a lot of other things too. Mainly with libraries (statically or dynamically linked). When you write code to use one of these libraries, the compiler needs to know which arguments the functions take, which variables the classes have, etc. This is the basic idea:

1. Headers are meant to define things.
2. Source files are meant to implement things.

You can import headers with #include "file.h", and global headers (like iostream) with #include <file(.h)>.
For all STL headers (the ones that use the namespace std) you can leave out .h.

Let's take the example of the code in multiple files again. You define the functions in the second cpp file in a header, and include the header in the first one. Now it is possible to call the function from the first cpp file.

Enough theory, let me show you an example.

Main.cpp:
Code:
 

#include "add.h"
#include <iostream>

using namespace std;

int main() {
cout<<add(1,2);
return 0;
}

Add.cpp:
Code:
 

#include "add.h"

int add(int a, int b) {
return a + b;
}

Add.h:
Code:
 

#pragma once

int add(int a, int b);

So how does this work? You describe the function's in and output in the header file, and implement them in the source file. Note that #pragma once prevents the header from being importer multiple times per source file.
It is optional to give the arguments names in a definition. You could as well write:
Code:
 

int add(int,int);


This works the same for classes/structs. You define them in header files, but put no code in them. Only implement things in source files. Don't put using namespace in a header file, because all files using that header will also use the namespace.

You may be wondering "What happens when I do put code inside a header file?" Well, your compiler will complain. Code is compiled down to object (.o or .obj) files. One for each source file. When all source files are compiled, the linker links all object files together in one file. Header files are included once per source file (when using #pragme once), not once in the entire project. So what happens if you do put code inside the header file, is that it gets compiled multiple times, in different object files. When the linker tries to link the object files, it will find the same code in multiple object files. This results in a conflict. So never try to put code in your header files, only definitions, no implementations.

There are multiple ways to include header files. This is the proper way: 1.
There is also a quicker but dirtier way: 2.

Posted Image


I hope you learnt something ;)

~Ruud.

Veniogames
Vēnit, ut mē occidĕret.

I will not use Ultimate3d 2.x.x anymore - I am an Ogre C++ programmer.
Offline Profile Quote Post Goto Top
 
Dr. Best
Member Avatar
Administrator
[ *  *  *  *  *  * ]
Good tutorial. I remember that I really needed something like this years ago when I started using C++ ^_^ . There is one thing I want to add though. You said that there have to be only function declarations in header files, no function definitions, but there are several exceptions from this rule:

  • All inline functions have to be defined at the place at which they are declared. It is valid to place their definitions in header files.
  • The same applies to template functions. They have to be defined directly after the declaration, so they can be defined in header files.
  • Member functions of classes or structures can always be defined in the header, right after the declaration, although this should be done only for very short functions, to keep the definition of the class clear.
Offline Profile Quote Post Goto Top
 
Ruud v A
Member Avatar
Programmer · Artist
[ *  *  *  *  * ]
Once again you are right...

Veniogames
Vēnit, ut mē occidĕret.

I will not use Ultimate3d 2.x.x anymore - I am an Ogre C++ programmer.
Offline Profile Quote Post Goto Top
 
skarik
Member Avatar
kitten eating scum
[ *  *  *  *  *  * ]
Oh my god.

Code:
 
// What Skarik could be doing...
#pragma once
...code...


Oh my god.

Code:
 
// ...and what Skarik does.
#ifndef THIS_HEADER_FILE
#def THIS_HEADER_FILE
...code...
#endif











Yeah, I like my way better. It looks cooler.
Blog|EHS
Offline Profile Quote Post Goto Top
 
« Previous Topic · Tutorials · Next Topic »
Add Reply