www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/08/22:18:14

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help With Include Files.....PLEASE!
Date: Sat, 08 Feb 1997 14:46:59 -0800
Organization: Two pounds of chaos and a pinch of salt
Lines: 52
Message-ID: <32FD0263.4DC1@cs.com>
References: <32FC8E53 DOT 77A4 AT praxis DOT net>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp104.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


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