Click here to Skip to main content
15,885,941 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
Updated 17-Jan-13 4:35am
v2

It should be something like:

foo.h
C++
// Header file foo.h
// declare a to be a global variable
extern int a;


main.c
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
C++
// other source file which uses the value of a
#include "foo.h" // gets the definition of a

void bar(int value)
{
    a += value;
}
 
Share this answer
 
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:
C++
// Header file a.h

// A global variable that can be accessed from other modules.
// Don't assign a value here.
extern int a;

C++
// 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:
C++
// 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.
 
Share this answer
 
Comments
nv3 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 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 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900