Multi-file linkage
I have 2 source files: WinMain.cpp and DDraw.cpp
I would like to use a header with all the globals stored in there (I think that's the way most people do it). I get a problem when I attempt that, because the variables are declared twice if I include the file in both source-files, and it gives me link errors. How might I set up 1 header file to handle the globals for all the other source files? If anyone can show me some small code examples I would greatly appreciate it.
Edited by - Laroche on November 27, 2001 9:30:24 PM
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
I think you need to define the variable once somewhere and then in the header file put an extern in front of the declaration.
Like in one of the source files put:
int whatever;
Then in the header file put:
extern int whatever;
Like in one of the source files put:
int whatever;
Then in the header file put:
extern int whatever;
At the top of the header put
#ifndef __MYHEADER__
#define __MYHEADER__
//code goes here
#endif
Of course you can replace __MYHEADER__ with whatever the hell you want. The purpose of all that is to have the preprocessor check if the macro __MYHEADER__ is defined. If it isn''t, it defines it and includes the following code. If it''s already defined, everything between the #ifndef and the #endif is ignored.
So the first time the file is processed __MYHEADER__ gets defined and the code is included. During the second pass the #ifndef fails so nothing gets redefined.
#ifndef __MYHEADER__
#define __MYHEADER__
//code goes here
#endif
Of course you can replace __MYHEADER__ with whatever the hell you want. The purpose of all that is to have the preprocessor check if the macro __MYHEADER__ is defined. If it isn''t, it defines it and includes the following code. If it''s already defined, everything between the #ifndef and the #endif is ignored.
So the first time the file is processed __MYHEADER__ gets defined and the code is included. During the second pass the #ifndef fails so nothing gets redefined.
The header file for the global variable should look something like this:
_And you should have a source file
You then include the source file in your project and the linker hooks everything up.
Magmai Kai Holmlor
- Not For Rent
Edited by - Magmai Kai Holmlor on December 1, 2001 12:03:48 AM
//Global.h#include <ddraw.h> extern IDirectDraw7* g_pDirectDraw7;
_And you should have a source file
//Global.cpp#include <Global.h> IDirectDraw7* g_pDirectDraw7=0;
You then include the source file in your project and the linker hooks everything up.
Magmai Kai Holmlor
- Not For Rent
Edited by - Magmai Kai Holmlor on December 1, 2001 12:03:48 AM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement