Mail Archives: djgpp/1998/08/28/01:47:41
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 -