Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
Currently learning about the extern variable .
 
I have declared a variable called
 
int a=10
in a header file head.h
 
int b=10
in a source file file.c
 
In file1.c
 
I used
extern int b;
 
and printed the variable b using printf.
 
when i use
extern int a
.It shows an error saying it cannot find the referenced variable . Is it something to do with compiler or ANSI C that does not look up for references in header when extern is used?
 
I have to use #include "head.h" to print variable a.
 
Can anyone explain whether its ANSI or visual studio problem?
Posted 17-Jan-13 3:40am
Edited 17-Jan-13 4:35am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It should be something like:
 
foo.h
// Header file foo.h
// declare a to be a global variable
extern int a;
 
main.c
// Main source file, which contains the actual variable a
#include "foo.h"

int a; // declare the variable as part of this module
 
bar.c
// other source file which uses the value of a
#include "foo.h" // gets the definition of a

void bar(int value)
{
    a += value;
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The problem is neither ANSI nor VS.
 
You must differentiate between definitions and code (.c files), and declarations (in .h files).
 
Handle global variables just like handling global functions. Declare them in a header file and define them in the corresponding source file:
// Header file a.h

// A global variable that can be accessed from other modules.
// Don't assign a value here.
extern int a;
// Source file a.c 
#include "a.h"

// Assign value to global variable.
int a = 10;
If you now want to use the global variable a from within another module, include the header file:
// File b.c
#include "a.h"

printf("a = %d\n", a);
 
If you put something like 'int a = 10;' into a header file, each file that includes this header file will have its's own copy of the variable. Then it is not global, but local to each module.
  Permalink  
Comments
nv3 at 17-Jan-13 11:20am
   
Your general advice is very good.
 
But ... Quote: "If you put something like 'int a = 10;' into a header file, each file that includes this header file will have its's own copy of the variable."
 
That is not true if I recollect C / C++ rules correctly; this will just lead to multiple definitions of the same global variable and the linker might complain. But it will be the same global variable that is referred to.
Jochen Arndt at 17-Jan-13 11:38am
   
Thank you for the clarification. The sentence is not correct in this form. I think this would be correct: The compiler will place an instance of the variable in each object file and the linker will complain about multiple symbols.
nv3 at 17-Jan-13 11:51am
   
That sounds good to me. Btw. I am not certain that all linkers will complain. Some might even be so smart to ignore the issue as long as the initializer is the same in all cases (which would be the case here).
Jochen Arndt at 17-Jan-13 12:07pm
   
Maybe someone with a deep knowledge of linking processes may help. But I don't think that a linker will use only one instance because he does not know about the initialization (OK, he might read it from the object file).
Sergey Alexandrovich Kryukov at 17-Jan-13 11:59am
   
My 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid