|
🔒 Locked
Header Files.
Advertisement
Original Post
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
..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]
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;
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;
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]
lol.. honestly that article isn't that great.
The 'extern' command was what I was guessing at; just wasn't sure.
Thanks~ [smile]
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
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
|
Even though my other "Source Files" include this header file, they have errors when accessing some of the members of the structure.
data.c
|
data.c(155) : error C2037: left of 'bytType' specifies undefined struct/union 'strCreature'.
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
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...
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.
data.h(7) : error C2011: 'strCompletedLvls' : 'struct' type redefinition
That occurs when two "Source Files" include this header...
|
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.
As stated above, you need some inclusion guards.
Either "#pragma once" at the start of the file, or
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
Either "#pragma once" at the start of the file, or
|
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
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement