help with headers!
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?
header.h
source1.c
source2.c
#ifndef someheader#define someheaderextern 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; }
If your CPPs look something like this:
someheader.h should look something like this:
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
#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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement