Mail Archives: djgpp/1997/02/10/14:15:06
From: SoMeOnE <bradh AT praxis DOT net>
Newsgroups: comp.os.msdos.djgpp
Date: Sat, 08 Feb 1997 09:31:47 -0500
Thanks for reading this message....my problem is when I try to make and
include file ( .h ) and include it in multiple C++ files...it comes up
with an error on all int's and char's that they have already been
defined....I have always had this error with other compilers and I know
it's me that's doing something wrong, not the compiler. If someone could
reply to this and tell me what I am doing wrong, I would greatly
appreciate it....
You can only define an object once, it may be declared as frequently as needed.
The best way to accomplish this is with a #if defined() in the header and a
#define in the main source file:
################################################################################
mymodule.h:
#if defined( MyModuleMain )
# define EXTERNDEF
int InitializedInt = 100; /* Initialized definitions like this in the #if */
char StringVarWithString[] = "This is an initialized char array.";
#else
# define EXTERNDEF extern
extern int InitializedInt;/* Declaration of initialized vars in the #else */
extern char StringVarWithString[];
#endif
/* Any uninitialized variable definitions/declarations can be done this way */
/* to save a little typing rather than duplicating each as for the initialized */
/* variables above. */
EXTERNDEF int GlobalInt;
EXTERNDEF char ArrayOfChar[12];
################################################################################
mymodule.c:
#define MyModuleMain 1
#include "mymodule.h"
...
...
...
################################################################################
othermodule.c:
#if defined(MyModuleMain) /* Obviously this is not neccessary, but it /*
# undef MyModuleMain /* illustrates the point. Documents the behavior too! */
#endif
#include "mumodule.h"
...
...
...
################################################################################
--
Art S. Kagel, kagel AT quasar DOT bloomberg DOT com
A proverb is no proverb to you 'till life has illustrated it. -- John Keats
- Raw text -