delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/28/01:47:41

From: horst DOT kraemer AT snafu DOT de (Horst Kraemer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Problem with include-files
Date: Fri, 28 Aug 1998 05:31:57 GMT
Organization: [Posted via] Interactive Networx
Lines: 70
Message-ID: <35e595c5.102036159@news.snafu.de>
References: <35e443c2 DOT 92340 AT news DOT space DOT net>
NNTP-Posting-Host: n243-87.berlin.snafu.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Wed, 26 Aug 1998 17:23:08 GMT, califax AT wupperonline DOT de (Klaus
Petzold) wrote:

>I have a problem under DJGPP version 2.7.2.1 when I want to implement
>something like the following:

>->filename: test.h

>#ifndef _TEST_
>#define _TEST_

>int t;

>#endif

>->filename test.cc

>#include "test.h"


>-> filename main.cc

>#include "test.h"

>int main()
>{
>}

>When I link these files I get the following error message:
>-multiple definition of 't'
>What's wrong?

1) Never define macro names starting with an underliner. In C names
beginning with an underliner are reserved for the system.

The problem is that both main.cc and test.cc will contain a definition

	int t;

i.e. you have two int variables at two different locations with the
same name.

I'm not sure which functionality you _wanted_ to implement. If you
want to use a global variable 't' in several files you have to
_declare_ it in every file using "extern" and to _define_ it in
exactly one file omitting "extern".

Thus the header file should look like this

	extern int t;

and either in main.cc or in test.cc you would have to define it

        #include "test.h" /* not necessary in the defining file,
                             but recommended */
	int t;

Loosely speaking

	extern int t;

says "there is an int t somewhere"

	int t;

says "int t is here"

Regards
Horst

- Raw text -


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