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

From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help With Include Files.....PLEASE!
Date: Sat, 08 Feb 1997 18:39:24 GMT
Organization: Oxford University
Lines: 48
Message-ID: <32fcc56b.5659548@news.ox.ac.uk>
References: <32FC8E53 DOT 77A4 AT praxis DOT net>
NNTP-Posting-Host: mc31.merton.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Sat, 08 Feb 1997 09:31:47 -0500, SoMeOnE <bradh AT praxis DOT net> 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....

You have to put the variable definition into exactly one .cc file, and
'extern' the declaration in the .h file.

e.g.

--- file1.cc ---
#include "file1.h"
#include "file2.h"

char something;

int main (void)
{
 something=1;
 printit();
 something=2;
 printit();
 return 0;
}

--- file2.cc ---
#include <stdio.h>
#include "file1.h"

void printit()
{
 printf("%d\n",something);
}

--- file1.h ---
extern char something;

--- file2.h ---
void printit();

--- end ---

Note that the header file says 'extern ...' and the .cc file just has
the definition.

George Foot

- Raw text -


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