Archived

This topic is now archived and is closed to further replies.

Load Runner

Files

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites