Click to See Complete Forum and Search --> : why compile header files


cspears
01-02-2005, 08:38 PM
I've been teaching myself C++. Why do you need to compile .h files in order to use them? I thought these files are just being read.

madcompnerd
01-02-2005, 10:17 PM
You shouldn't be compiling them.

cspears
01-02-2005, 11:01 PM
For some reason, my code didn't work until I compiled them.

evac-q8r
01-03-2005, 01:18 AM
Your are right... the files are read by the include statement and then are expanded within the .c file that includes it.

EVAC

truls
01-03-2005, 04:49 AM
What exactly do you mean by "compiled them"?

Could you please post the makefile, or the command line you are using to compile the program. Do you mean include them?

cspears
01-03-2005, 12:15 PM
I first used this:

g++ Employee.h

Then I did this:

g++ -o MakeAnEmployee MakeAnEmployee

madcompnerd
01-03-2005, 05:07 PM
Have you defined any objects in your .h files?

void foo(void);
Is not an object.

void foo(void)
{
printf("foo!");
}
Is an object.


struct bar
{
int j;
};
Is not an object.

struct bar *x = (struct bar *)malloc(sizeof(struct bar));
Is an object.

You must compile objects, but not headers. Objects should only exist in your .c files.
Don't get thrown off by my use of the word object, I'm aware this is c and not c++. I couldn't think of a better word to describe a defined element verse a note that a element will be later defined.

madcompnerd
01-03-2005, 05:08 PM
Are you including other project files in your .h? You should never include anything but .h files as a general rule.

cspears
01-03-2005, 05:26 PM
Employee.h is the header file referenced by MakeAnEmployee. You probably have figured this out, but I decided to clarify it for others. Employee.h contains a class, which I think is an object, right? That is probably why I needed to compile it.