A global variable defined with the same name in such a library

I wanted to know the behavior in the following scenario: -

//file1.c : Main file of a user-space process,say Process X.
int a; //GLobal variable in file1.c
func(); //Library function

//file2.c :Part of .so used by Process X.
int a;
void func()
{
    a=0;//Access variable a.
}

If process X calls a func()library function , what happens?

+4
source share
2 answers

In file1.cyou have identified

int a;

which tells the compiler to allocate memory for ain this compilation module, all references to it awill be resolved there by the compiler (and not the linker). So, file1sees his own aand file1sees his own a. If you used instead,

extern int a;

file1, , a file2.c.

file2 , a , file2.so file2.h,

extern int a;

file2.h #include d file1.c.

+2

. .

a 2 func, a 1 . .

0

All Articles