Skip to main content
gamedev.net
🔒 Locked

Header Files.

Started by Fleury Jul 1, 2004 at 12:43 PM 11 replies 2.3k views
Original Post
Fleury
Fleury
Naturally, I love organizing my project into several smaller files; so I gave header files a try. main.h main.c physics.h physics.c graphics.h graphics.c data.h data.c eg.. the layout of my project. Every *.c inports its *.h file. For example, Data.c includes Data.h, and Physics.c includes Physics.h. And in each of these *.c, is an include statement to 'main.h'. My 'main.c' includes all of the header files. My problem is understanding Header Files in general; I've put some of my global variables in the header files, such as... main.h
#include <windows.h>

/*  Function Prototypes  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void sLoadPicture (char strFileName[], HDC *TheDC);
int bolDetectCollision(int intX1, int intY1, int intWidth1, int intHeight1,
                        int intX2, int intY2, int intWidth2, int intHeight2);


/*  Windows Information */

HWND hwnd; /* Our Window Handle */
HDC WindowDC;

/* Location of mouse click. */
short int intMouseClickX;
short int intMouseClickY;

/* Surfaces */
HDC hdcMenu;
HDC hdcBackBuffer;
HDC hdcButtons;
HDC hdcObjects;
HDC hdcTiles;

/* Stupid Win32 */
PAINTSTRUCT ps;    

/* Game Varibles */
unsigned char intProgramLocation;
char chrTempName[10];
..and since all the other *.c files include this header, does that mean there are multiple instances of these variables in each of the files? I get this error. physics.obj : error LNK2005: _bolSwitch2 already defined in main.obj [headshake]
doynax
doynax
Yes. You have to declare the data using 'extern' in the header and place the definition in the source file.

// headerextern int x;// sourceint x;
Fruny
Fruny
Article
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
mikeman
mikeman
It would be best to avoid as much as possible the use of global variables.Now,the way to it is:

1)In your .c file you define the variable:

int GlobalVariable;

2)In your header file you put an external declaration,to tell the compiler that the variable is defined in another file:

extern int GlobalVariable;
Fleury
Fleury
I've read the article once before, and thought it irrelevent to my problem. Upon reading it again, I noticed on page 4, at the very bottom, it actually explains my problem.

lol.. honestly that article isn't that great.

The 'extern' command was what I was guessing at; just wasn't sure.

Thanks~ [smile]
Kylotan
Kylotan
Quote:
Original post by Fleury
I've read the article once before, and thought it irrelevent to my problem. Upon reading it again, I noticed on page 4, at the very bottom, it actually explains my problem.

lol.. honestly that article isn't that great.


Wise man say: don't criticize article when you're making obvious mistakes that it plainly tells you how to address.

PS. These people mostly disagree with you. :P
Fleury
Fleury
Quote:
Original post by Kylotan
Quote:
Original post by Fleury
I've read the article once before, and thought it irrelevent to my problem. Upon reading it again, I noticed on page 4, at the very bottom, it actually explains my problem.

lol.. honestly that article isn't that great.


Wise man say: don't criticize article when you're making obvious mistakes that it plainly tells you how to address.

PS. These people mostly disagree with you. :P


Well.. it doesn't plainly tell me, hence why I critised it.

Anyways, I have another question about headers; I wrote a structure in a "Source File" and in my headerI put this..

physics.h
extern int bolDetectCollision(int intX1, int intY1, int intWidth1, int intHeight1,                        int intX2, int intY2, int intWidth2, int intHeight2);                       /* Projectiles & Powerups, use this structure. */ extern struct strProjectile;/* All creatures, including the players, use this structure. */extern struct strCreature;


Even though my other "Source Files" include this header file, they have errors when accessing some of the members of the structure.

data.c
    /* Open the File*/    f = fopen(strCharName,"wb");    	/* Name */    fwrite(&strCharName,10,1,f);    	/* Class */	fwrite(&TheCreature->bytType,1,1,f);	/* Gold */	fwrite(&TheCreature->TheAttributes->intGold,4,1,f);	/* Level */	fwrite(&TheCreature->TheAttributes->bytLevel,1,1,f);


data.c(155) : error C2037: left of 'bytType' specifies undefined struct/union 'strCreature'.
Miserable
Miserable
A forward declaration isn't good enough when you need to access members; you must define the struct in the header. Note that you are not defining an actual instance, so it is not analogous to an extern global.
Fleury
Fleury
Quote:
Original post by Miserable
A forward declaration isn't good enough when you need to access members; you must define the struct in the header. Note that you are not defining an actual instance, so it is not analogous to an extern global.


Interesting.. I would have guessed that multiple definitions of a structure would cause an error.

I did what you said, and it worked. Thanks. :D
Spudder
Spudder
Inclusion guards will prevent the struct from being defined more than once.
Fleury
Fleury
Almost got this game functioning now... however I ran into that "redefinition" error.

data.h(7) : error C2011: 'strCompletedLvls' : 'struct' type redefinition

That occurs when two "Source Files" include this header...


/* Because I couldn't create an array of arrays without   it being two dimensional; which I didn't want. I created   an array of structures; which worked. */struct strCompletedLvls{	char chrName[10];};


I did a search on "Inclusion Guard"; could have guessed it would lead me back to that "Article". I included #pragma once, and it fixed it, but I recall one of you stating it won't work for my case, so I await further errors.
ch1pz
ch1pz
As stated above, you need some inclusion guards.

Either "#pragma once" at the start of the file, or

#ifndef BLAH#define BLAHcode#endif


will do it if I understand what you've done...

edit: Did you just add that bit at the bottom about #pragma once, or did my brain just manage to totally ignore it the first few times I read it?

Oceania has always been at war with Eurasia
Fleury
Fleury
Added it. :P

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.