Archived

This topic is now archived and is closed to further replies.

the header files?

This topic is 5510 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone explain to me little bit about the header files? Every tutorial and my book just say that its nothing to worry about at this point. Well i want to know. Why do we need them? What is in those files? How do i create one? Is it just txt file or what? Just anything that you know about them would help me. Or if you have some cool link where it would explain it to me.

Share this post


Link to post
Share on other sites
Well lets first start with function declarations/prototypes:
this is for instance a function declared above main, ex.:

int addNumbers(int n1, int n2);
int main(void)
{
blah blah blah
}

int addNumbers(int n1, int n2)
{
return(n1+n2);
}

now a header file for the most part is made up of a bunch of function declarations (the line above main in the above code), when you #include a header file it is the same as copying all the function declarations in that header file, into the space above main. So it''s purpose is for reusability (so you don''t have to retype all the function declarations) and for organization (similar function declarations are in the same header file)

Share this post


Link to post
Share on other sites
So lets say i have 10 functions which i use in most of the programs i make. I can just create a header file with all the prototypes. And i also have write the actuall function in the header file, right?

So how do i create header file? Is it just a txt file with extension *.h at the end???

Share this post


Link to post
Share on other sites
So lets say i have 10 functions which i use in most of the programs i make. I can just create a header file with all the prototypes. And i also have write the actuall function in the header file, right?

So how do i create header file? Is it just a txt file with extension *.h at the end???

Share this post


Link to post
Share on other sites
You never compile header files, only the actual .c/.cpp files. If you only have one code file (.c/cpp file) there is no real need of using your own header files. It is when you divide your code into several code files, that need to use stuff from eachother, that the need of header files arise. Understand that the compiler process each .cpp file individually and loose all declarations etc. in between. The result is a few object files, which are then put together by the linker.

Consider the standard functions you''re using (like printf() etc.). Their compiled code is located in libraries. When you compile your code, the compiler needs to know how all these functions look (their prototype/signature). These are located in the standard header files <stdio.h> etc.

So: no, you shall not ever put actual code in header files; they shall only contain various declarations (functions, typedefs, structs, classes). Header files are normal "text files", and you make them like normal .cpp files but with the extension .h. When you use an #include "blabla.h" directive, you tell the preprocessor to insert the content of that file instead of the directive. Remember that the <> when include standard headers tells the compiler to look for the file in its path, and you should use quotes "" when including your own headers.

Btw, any decent C/C++ book should explain this. Most have a chapter dedicated to this stuff. A free book I can recommend that explains this in detail is Thinking in C++, by Bruce Eckel0. It''s a complete two volume, that you can download for free as PDF.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Actually you CAN put code in your .h files. Usually you don''t because the whole point of them is to only give you and other files the info you need. I guess the most common reason to put code into the header is functions that are to be inlined. I believe by default, at least with the VC++ compiler, if you put the body of a function into the header it tries to inline the function.

Share this post


Link to post
Share on other sites
i am going to play (i am for real) really stupid...

quote:
Remember that the <> when include standard headers tells the compiler to look for the file in its path


what do you mean by IN ITS PATH?

1.)for example . How does the compiler knows where to look for the functions whose prototypes are in this header file or any other header file that i would include???

2.)Lets say i create my own header file for program A. Then, lets say i create program B and i want to use the functions that are in the header file that i have created for program A. How does the compiler knows where to look for the functions? Do i have to include progrm A with program B??? or do i just have to include the class with functions???

*confused*


Share this post


Link to post
Share on other sites
I''m currently working on my Terrain for a game and I have the following:

A header file with a class definition off Terrain, something like:

Class Terrain:
{
Public:
LoadHeightMap(char* Path)
GetHeight(x,z)

Terrain()
~Terrain()
....
Private:
int Cols
int Rows
...
}

In my file Terrain.cpp I have:

Terrain::Terrain()
{
//The init of my terrain
}
Terrain::~Terrain()
{
// destructor
}
Terrain:GetHeight(x,z)
{
return pHeightMap(x+(z*cols)
}

(the code is pseudo code so don''t try to compile it )

If I want to reuse my terrain I would copy the Terrain.cpp and the Terrain.h file. If I include the Terrain.h file in my program I can use all the functions I have specified there and which are worked out in the cpp file..

Share this post


Link to post
Share on other sites
quote:
Original post by bozkosko
what do you mean by IN ITS PATH?
The "path" is the directories a program will search when opening a file.
quote:
1.)for example . How does the compiler knows where to look for the functions whose prototypes are in this header file or any other header file that i would include???¨

For a compiler, there is something known as the "include path" which is a list of directories on your computer where the compiler will look for files you try to include in the code. There is also a "lib path", which is where static libraries are located, and are used by the linked.

Stuff like iostream, is compiled into static libraries, and the prototypes of the functions within that static library are typed out in header files, that exists in the compilers standard include path. When you have a project, you must tell it explicitly which libraries to link with. If you don't, you'll get a linker error. Most compilers/IDEs usually have the most common libraries by default, so you don't need to specify them.

How the compiler knows all this? If you're using MS VC++, you find the various paths in Tools->Options->Directories, and the libraries to link with somewhere in the Project Settings.
quote:
2.)Lets say i create my own header file for program A. Then, lets say i create program B and i want to use the functions that are in the header file that i have created for program A. How does the compiler knows where to look for the functions? Do i have to include progrm A with program B??? or do i just have to include the class with functions???
If you just want to reuse code, then copy the files (or add them to your project). What you're talking about is more like creating a static library, which I don't think you should attempt right now.
quote:
*confused*
hehe... aren't we all?

I would really recommend you to study Chapter 3 in the book i refered to above (Thinking in C++), which is really essential to be able to do anything. Unless you understand how the compiler interprets your files, and the entire building process, you will forever be confused. Learn it, once and for all, and world is yours

[edited by - CWizard on November 11, 2002 12:48:05 PM]

Share this post


Link to post
Share on other sites