Files

Started by
10 comments, last by Load Runner 22 years, 6 months ago
Hi. I was just wondering how I should go about splitting my project up in several sourcefiles and use only one headerfile with all the globals. i know i should use #ifndef and stuff but im not really clear on this any help is appriciated
Romance is dead,it was bougth by Disney and Hallmark and sold to the public in small portions.
Advertisement
if you want to put all of your globals in one header and use that header in your multiple source files, then you will want to use inclusion guards in the global header. yeah, you are on the right track with the ifndefs. here's an example

global.h
#ifndef _GLOBAL_H#define _GLOBAL_H// define your functions and variables#endif  


what this does is check and see if _GLOBAL_H is defined, and if it's not it defines it and defines the rest of the variables and functions. if _GLOBAL_H is already defined, the variables aren't declared. you don't necessarily have to use the beginning underscore, but it just helps you make sure that there isn't already some other #define called that. not necessarily in your code, but in library headers and such.

anyway, this should be what you need.

dave


--
david@neonstar.net
neonstar entertainment

Edited by - neonstar on October 16, 2001 4:03:28 PM
--david@neonstar.netneonstar entertainment
Thanks for your reply neonstar

i did exactly what you showed but it still give me errors:

error LNK2005: "blah blah" already defined in WinMain.obj

any ide what could be wrong?
Romance is dead,it was bougth by Disney and Hallmark and sold to the public in small portions.
what i gave you there should have prevented from that problem. just remember to keep the #endif in the header file after all of your variable and function declarations and the #ifndef and #define before them.

ah, i just thought of something. some variables you have to define as extern in the header file, then define again in the main source file they will be used in. for example:

in global.h

extern char ProgramName[100]; 


and in main.cpp
char ProgramName[100]; 


you won''t have to define it again in every source file. just define it in one and it will be fine. function declarations will be fine, but variables are funny in that respect. let me know if you have more problems or don''t understand what I did here.

dave

--
david@neonstar.net
neonstar entertainment
--david@neonstar.netneonstar entertainment
Hi
thanks again
one more question tho
would it be possible to put all the global variables in a struct
and then make the struct itself extern?
Romance is dead,it was bougth by Disney and Hallmark and sold to the public in small portions.
yeah, you can put then all in a struct, but don''t make the struct declaration extern. when you define a variable from the struct, then you should extern. like so:

struct global_stuff{   int number;   char letter;} ;extern global_stuff globals; 


hope that helps.

dave

--
david@neonstar.net
neonstar entertainment
--david@neonstar.netneonstar entertainment
Thanks for your help neonstar, it all works now
Romance is dead,it was bougth by Disney and Hallmark and sold to the public in small portions.
hey, no problem. moving your globals around can really be a pain in the ass and i''ve had my share of problems with them. just trying to share the info

dave
--david@neonstar.netneonstar entertainment
Teej had a nice little trick:
#ifndef GLOBALS_OWNERSHIPextern#endifglobal_stuff globals; 


Then in the file you want to own the globals structure:
#define GLOBALS_OWNERSHIP 
There is an extremely simple solution to this problem, you know...

"A society without religion is like a crazed psychopath without a loaded .45"
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement