Archived

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

help with headers!

This topic is 5148 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basicly my program is running in two main .cpp files at the moment and I''m trying to work out how to have a few globals like hdc and hwnd to be shared between them. I tried including the globals in header files but all that seems to do is create linker errors even with the ifndaef statements and such. So how do I get globals declared in one file to be visible to other files?

Share this post


Link to post
Share on other sites
header.h

#ifndef someheader
#define someheader

extern int somevar;

#endif


source1.c

#include "header.h"

int somevar = 5;


source2.c

#include "header.h"
#include <stdio.h>

int main(int argc, char *argv[])
{
printf("value of somevar: %d", somevar);
return 0;
}

Share this post


Link to post
Share on other sites
If your CPPs look something like this:


#include "someheader.h"
HWND g_windowHandle;

int main()
{
return SomeFunc(1337);
}



#include "someheader.h"
int SomeFunc(int value)
{
return value * value + (int)g_windowHandle;
}


someheader.h should look something like this:
extern HWND g_windowHandle;
int SomeFunc(int value);


Declare your globals (if you have to use them, it''s usually better to avoid them) in the CPP files, do the same in the H, except you put "extern" in front of the declaration.
Same with functions, except you don''t put "extern" in front of them in the H, just omit the function body and end the function header with a semicolon.


Member of the Un-Ban nes8bit Association (UNA) (to join put this in your sig)

We are at war with Iraq. We have always been at war with Iraq. I love Big Brother.
- DakeDesu

Share this post


Link to post
Share on other sites