www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/10/14:15:06

From: kagel AT quasar DOT bloomberg DOT com
Date: Mon, 10 Feb 1997 13:47:51 -0500
Message-Id: <9702101847.AA01155@quasar.bloomberg.com >
To: bradh AT praxis DOT net
Cc: djgpp AT delorie DOT com
In-Reply-To: <32FC8E53.77A4@praxis.net> (message from SoMeOnE on Sat, 08 Feb 1997 09:31:47 -0500)
Subject: Re: Help With Include Files.....PLEASE!
Reply-To: kagel AT dg1 DOT bloomberg DOT com

   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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019