microsoft visual studio - simple doubt 
Author Message
 microsoft visual studio - simple doubt

HI

I have constructed a win32 console application with three files
a.c (includes a.h)
b.c (includes b.h)
a.h

When i build it under the visual studio, i get linker errors saying that
some global variables decalred in a.h are multiply defined in the two obj
files a.obj and b.obj... How do i rectify this? Please help

Thanks
Raghu



Sun, 03 Nov 2002 03:00:00 GMT  
 microsoft visual studio - simple doubt

Quote:

>HI

>I have constructed a win32 console application with three files
>a.c (includes a.h)
>b.c (includes b.h)
>a.h

>When i build it under the visual studio, i get linker errors saying that
>some global variables decalred in a.h are multiply defined in the two obj
>files a.obj and b.obj... How do i rectify this? Please help

1. Do not define variables in .h files.
2. Do not extern variables in .h files that share the same name as
   globals in another .c file.

If you don't need a variable in any other files, then use
static in front of the variable definition:

static char thisModuleVar;

Your .h files should ALWAYS have extern in front of variables
as in

extern char allModulesVar;

which would be defined in one and only one .c file as

char allModulesVar;

- Mark

P.S. I've yet to really need global variables at all. Use
     access functions instead.



Sun, 03 Nov 2002 03:00:00 GMT  
 microsoft visual studio - simple doubt

Quote:

>HI

>I have constructed a win32 console application with three files
>a.c (includes a.h)
>b.c (includes b.h)
>a.h

That looks like four files to me.

Quote:
>When i build it under the visual studio, i get linker errors saying that
>some global variables decalred in a.h are multiply defined in the two obj
>files a.obj and b.obj... How do i rectify this? Please help

Those variables are not only declared in these headers but also defined.

A program must have at most one definition of an external object or function.

To write an external declaration  of an object such that it's not also
a definition, you must use the extern storage class specifier
and *omit* an initializer:

    extern int foo;

if you add an initializer, as in

    extern int foo = 3;

the declaration becomes a definition.

Anyway, once you have a declaration in the header, you must also define
the object somewhere. If the program uses the object, and the object
doesn't have a definition anywhere, the behavior is undefined.

Choose an appropriate .c file and inside it place

    int foo;

or

    int foo = 3; /* or some other initial value */



Sun, 03 Nov 2002 03:00:00 GMT  
 microsoft visual studio - simple doubt

Quote:

>I have constructed a win32 console application with three files
>a.c (includes a.h)
>b.c (includes b.h)
>a.h
>When i build it under the visual studio, i get linker errors saying that
>some global variables decalred in a.h are multiply defined in the two obj
>files a.obj and b.obj... How do i rectify this? Please help

you must be declaring a global variable in a.h.  when a.h gets included
in a.c and b.c, you are effectively declaring the same global variable
in both modules.  this is not allowed.

you shouldn't really declare global variables in .h files which are
included from multiple .c files, for exactly this reason.

declare the variable in one of the .c files, and then declare it as
"extern" in the other .c file.  example:

  a.c
  ---
  int my_global_var;

  b.c
  ---
  extern int my_global_var;

---
"... What with you being his parents and all, I think that you could
be trusted not to shaft him."  -- Robert Chang, rec.games.board




Sun, 03 Nov 2002 03:00:00 GMT  
 microsoft visual studio - simple doubt
HI

Thanks a lot... I rectified the problem..

Raghu



Quote:

> >HI

> >I have constructed a win32 console application with three files
> >a.c (includes a.h)
> >b.c (includes b.h)
> >a.h

> >When i build it under the visual studio, i get linker errors saying that
> >some global variables decalred in a.h are multiply defined in the two obj
> >files a.obj and b.obj... How do i rectify this? Please help

> 1. Do not define variables in .h files.
> 2. Do not extern variables in .h files that share the same name as
>    globals in another .c file.

> If you don't need a variable in any other files, then use
> static in front of the variable definition:

> static char thisModuleVar;

> Your .h files should ALWAYS have extern in front of variables
> as in

> extern char allModulesVar;

> which would be defined in one and only one .c file as

> char allModulesVar;

> - Mark

> P.S. I've yet to really need global variables at all. Use
>      access functions instead.



Sun, 03 Nov 2002 03:00:00 GMT  
 microsoft visual studio - simple doubt

Quote:
>HI

>I have constructed a win32 console application with three files
>a.c (includes a.h)
>b.c (includes b.h)
>a.h

>When i build it under the visual studio, i get linker errors saying that
>some global variables decalred in a.h are multiply defined in the two obj
>files a.obj and b.obj... How do i rectify this? Please help

Ok, you have received good answers about sharing global variables. But the
true question is "do you really need those globals ?"

--
-hs- "Stove"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"Show us your source code, and let us, like
rapacious vultures half-crazed by starvation, rip it to shreds until
there's practically nothing left" --Richard Heathfield CLC



Sun, 03 Nov 2002 03:00:00 GMT  
 microsoft visual studio - simple doubt

Quote:

>HI

>I have constructed a win32 console application with three files
>a.c (includes a.h)
>b.c (includes b.h)
>a.h

>When i build it under the visual studio, i get linker errors saying that
>some global variables decalred in a.h are multiply defined in the two obj
>files a.obj and b.obj... How do i rectify this? Please help

You need to remember that include files are literally included. So
text you put in a header appears in every file you #include it in. If
you define a global variable in a header then you get two definitions.
This is not allowed.

The solution is to put the _definition_ in one of your C files and the
_declaration_ in the header, with the "extern" keyword. This tells the
compiler to look elsewhere for the defintion.

//a.h
extern  int x;

// a.c
#include "a.h"
int x;

// b.c
#include "a.h"

Quote:

>Thanks
>Raghu

--
Mark McIntyre
C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html


Sun, 03 Nov 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Microsoft Visual Studio.NET V.S. Microsoft Visual C++ 6.0

2. DDE name for Microsoft Visual Developer Studio 4.0(visual c++4.0)

3. Good book on Microsoft Visual Studio Visual C++, C#, VB

4. Doubt about Visual Studio Net

5. Visual Studio desktop layout (Visual Studio/Visual C++ 6)

6. e: *HELP* After install Visual studio .Net, the Visual Studio Enterprise 6.0 stops working

7. *HELP* After install Visual studio .Net, the Visual Studio Enterprise 6.0 stops working

8. Visual Studio 6 back to Visual Studio 5

9. *HELP* After install Visual studio .Net, the Visual Studio Enterprise 6.0 stops working

10. Microsoft to tease new Visual Studio.Net

11. Microsoft Visual Studio .Net Debugger Crashing

12. Anyone going to Microsoft Visual Studio.Net Developer Conference in Copenhagen (Denmark)

 

 
Powered by phpBB® Forum Software