help with headers!

Started by
2 comments, last by flukus 20 years, 5 months ago
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?
Advertisement
header.h
#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; }
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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
Cool, thanks for the replies, I''ll go try it now!

This topic is closed to new replies.

Advertisement