| 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: |
| Header files; How to use them | |
|---|---|
| Tweet Topic Started: Jul 19 2008, 10:24 AM (649 Views) | |
| Ruud v A | Jul 19 2008, 10:24 AM Post #1 |
|
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:
Add.cpp:
Add.h:
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:
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. ![]() 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. | |
![]() |
|
| Dr. Best | Jul 19 2008, 01:15 PM Post #2 |
|
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:
|
![]() |
|
| Ruud v A | Jul 20 2008, 12:24 PM Post #3 |
|
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. | |
![]() |
|
| skarik | May 12 2009, 07:29 AM Post #4 |
|
kitten eating scum
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
Oh my god.
Oh my god.
Yeah, I like my way better. It looks cooler. |
| Blog|EHS | |
![]() |
|
| « Previous Topic · Tutorials · Next Topic » |





![]](http://z1.ifrm.com/static/1/pip_r.png)





. 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:
9:02 PM Jul 11