Be a Supporter!

Making C header files

  • 588 Views
  • 8 Replies
New Topic Respond to this Topic
thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Making C header files 2005-02-11 23:36:48 Reply

I've been searching for info on this practically forever.
One of the half-way decent resources I had shut off, so that's no help.
And Google is slutty.

If somebody would be so kind, as to give me a link to a tutorial where I could learn to code C header files, it would be appreceated.

PS: If you even mention the word "Google" in your post, I will give you The Shocker, and give it to you using your own hand...
Just a warning.


omg.
Playstation Network tag: muffin-noodle
the empty set

CronoMan
CronoMan
  • Member since: Jul. 19, 2004
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to Making C header files 2005-02-14 03:01:18 Reply

What C compiler are you using?

I don't think I can be to much help in any other compilers than MS VC++ since that's the compiler I use.


"no sound in ass"

irritus
irritus
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Making C header files 2005-02-14 13:03:12 Reply

Header files are easy. I'm not saying this to make you feel bad for asking; on the contrary, I think you'll be surprised they aren't more complicated.

There is nothing special you have to do to make a header file. As far as C/C++ is concerned, all code is text. You could write C/C++ in Windows Notepad if you were so inclined, and it would make no difference to your compiler and linker.

Basically, the point in making a header file is to seperate the declaration of your functions/classes seperate from the implimentation. Header files can also be used for putting configuration you want to use throughout your program (ex. declaring global constants).

There is one thing you should always do in header files, though. You should always make it so a header file is only compiled once. There are a couple reasons for this, but as a matter of practice you should always do it even if those reasons don't apply. Here is how:

example.h
-------------------------------------------------------------
#ifndef _EXAMPLE_H_
#define _EXAMPLE_H_

declarations

#endif
Empty line, just to avoid getting warnings from some compilers
-------------------------------------------------------------

#ifndef is an if statement posed to the compiler. It says, "If _EXAMPLE_H_ is not defined (in your variable table), do everything after this until you hit an #else or an #endif." #define _EXAMPLE_H_ tells the compiler to add _EXAMPLE_H_ to its variable table. As a result, the previous if-not-defined statment will never be true again. Then the compiler goes through all of your code. The extra empty line before the end of file is just there to avoid compiler warnings; of course, it takes one keystroke to add and won't alter the final size of a compiled program, so why leave it out?

So, why is it important to put in this compiler if statment? This does two important things. First, it prevents recompiling the same code. This does not cause problems if you have more than one file including "example.h". Instead of compiling the header again, all new files including it are connected to the previously compiled code instead of having their own copy. Second, it prevents some of very aggrivating errors about redefinition. I can't think of great examples of why this would happen, but a common one would be if you declared constants in a header file.

Lastly, you should base the declaration on the file name. I checked for a declaration of _EXAMPLE_H_ in a file named "example.h". This is just an easy way to prevent human error. All of your files should have unique names, so using that in your declaration will prevent duplicating them. However, you could use anything after #ifndef and #define. The only important thing is that they have the identical entry for the file they're in.

Ravens-Grin
Ravens-Grin
  • Member since: Jun. 3, 2003
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Making C header files 2005-02-14 23:17:54 Reply

Hmm I like shockers....

So why don't you google search it?

White-Rhyno
White-Rhyno
  • Member since: Apr. 28, 2003
  • Offline.
Forum Stats
Member
Level 38
Blank Slate
Response to Making C header files 2005-02-15 00:43:53 Reply

At 2/15/05 12:06 AM, JesusCyborg wrote: stuff

God, they just let anybody post here nowadays...

Lordfat
Lordfat
  • Member since: May. 30, 2000
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to Making C header files 2005-02-15 20:12:02 Reply

Like they said, its really easy stuff. Basically its just a list of all the fuctions/variables in your class. You know a stack right? Well this is what a stack header file would look like:

#ifndef STACK_CLASS_H
#define STACK_CLASS_H

class STACK
{
public: // these functions are open to any program

// The constructors
STACK();
STACK(int size);

void Push(int element);
void Pop();
int Top();

bool Error();

~STACK(); // The Deconstructor

private:
//these variables can't be accessed by other programs

int *array;

int max_size;

int index;

bool error_flag;

}; //don't forget the ; at the end of the class

#endif

Thats the stack.h file. Now you would need a stack.cpp file that has the code for pop, push, top, and all the other functions. Throw both files into a project, and then you would be able to use a stack anywhere you want. Hope this helps.

CronoMan
CronoMan
  • Member since: Jul. 19, 2004
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to Making C header files 2005-02-16 04:47:44 Reply

I think he's talking about how to MAKE header files, with the library file 'n shit.


"no sound in ass"

irritus
irritus
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Making C header files 2005-02-16 15:24:55 Reply

At 2/16/05 04:47 AM, CronoMan wrote: I think he's talking about how to MAKE header files, with the library file 'n shit.

What, as in precompiled header files?

Sith-sasquatch
Sith-sasquatch
  • Member since: Feb. 8, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to Making C header files 2005-02-19 21:17:55 Reply

At 2/16/05 03:24 PM, irritus wrote:
At 2/16/05 04:47 AM, CronoMan wrote: I think he's talking about how to MAKE header files, with the library file 'n shit.
What, as in precompiled header files?

Well, even if he's not, I would be interested in learning to make libraries.