[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Problems with g++



>       I'm using g++ egcs-2.91.60 and I've had some problems with
> inclusions of a header file. For example, if I declare a variable
> in example.h and #includes it in example.c++ and in example2.c++
> (these two files reference that variable and #include that header
> file), I get an error message like this:
> 
>    /tmp/cc8IeZSz.o(.bss+0x0): multiple definition of `global'
>    /tmp/ccovurMu.o(.bss+0x0): first defined here
>    collect2: ld returned 1 exit status     
> 
>       I tried to use something like this:
> 
>    #ifndef GLOBAL
>    int global;
>    #define GLOBAL
>    #endif
> 
>    in the header file, but the #ifndef is not working. 
> 
>    What can I do to declare all the global variables in the
> header file and avoid the redefinition of them, when the
> header is #included by more than one .c++ file? 

 You must use extern in declarations. In C and C++ there's a difference
between declaring and defining. The header files should only declare the
variable. Only one .c file should define it. To mark a declaration you add
the extern keyword, like this:

extern int global;

 And in one .c you will need a:

int global;

 (or int global = 0; )

 The right extension for a c++ file is file.cc (or file.C).

 


Reply to: