Mail Archives: djgpp/1997/02/08/22:18:14
SoMeOnE wrote:
>
> 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....
This should be covered by your C references. You should never _define_
variables in header files unless that header file will only be included
in only one source file. The correct way to do this is to _declare_ the
variables in the header file with the 'extern' storage class, and define
them in one and only one source file.
Example:
-- header.h --
extern int array[10][10];
-- main.c --
#include "header.h"
-- stuff.c --
#include "header.h"
-- data.c --
#include "header.h"
int array[10][10] =
{
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
/* etc. */
};
-- end --
Basically, an external variable may be defined in one and only one
source file.
--
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I | fighteer AT cs DOT com |
| "Starting flamewars since 1993" | http://www.cs.com/fighteer |
| *** NOTICE *** This .signature is generated randomly. |
| If you don't like it, sue my computer. |
---------------------------------------------------------------------
- Raw text -