5

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 the Process X calls the function func() of the library, what will happen?

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • 1
    Interesting question! Maybe this other question relates something similar: http://stackoverflow.com/questions/7201667/ld-magically-overrides-statically-linked-symbols , for functions, though. As I read through some man pages (ld.so, dlopen), it seems variables may be handled differently, but I am no expert on this. Also waiting for a nice answer! – Jubatian Sep 19 '14 at 15:37
  • 1
    You understood the question right..:)..I am looking for something similar on global variables – Neelansh Mittal Sep 19 '14 at 16:49

2 Answers2

2

In file1.c you have defined

int a;

which tells the compiler to allocate memory for a in that compilation unit, an all references to a will be resolved there by the compiler (and not the linker). So file1 sees its own a and file1 sees its own a. If you had instead, used

extern int a;

in file1 then the compiler will defer resolution of this symbol to the linker, and then a will be resolved outside of file2.c.

Since file2 is a shared object, if variable a is supposed to be used by other files, then file2.so would likely come with a file2.h, which would have the line

extern int a;

and this file2.h would then be #included in file1.c.

Jay
  • 9,585
  • 6
  • 49
  • 72
  • file2.c is a shared object,linked and loaded dynamically to Process X. – Neelansh Mittal Sep 19 '14 at 15:21
  • "Since file2 is a shared object, if variable a is supposed to be used by other files, then file2.so would likely come with a file2.h, which would have the line" No. You're not getting the point..the other file is calling the function func() which accesses variable a.there is no need of externing the variable in the header file.The header file will just carry the prototype of func(). – Neelansh Mittal Sep 19 '14 at 16:41
  • no need to claim `extern`. it makes no difference between you claim it or not since `extern` is default state. – Jason Hu Sep 19 '14 at 16:53
0

have a test. so easy.

a in file2 is linked with func, so a in file1 won't be affected. they are different two variables.

Jason Hu
  • 6,239
  • 1
  • 20
  • 41
  • Note that both are "extern Global" variables.and file2.c is a shared lib – Neelansh Mittal Sep 19 '14 at 15:18
  • @NeelanshMittal that's what i am talking about. a in file2 won't be "dynamically" linked to a in file1 in this case. – Jason Hu Sep 19 '14 at 15:21
  • File2 is part of a shared object(dynamic lib)..it has to be linked dynimically – Neelansh Mittal Sep 19 '14 at 16:45
  • i was wrong. here you go. http://stackoverflow.com/questions/19373061/what-happens-to-global-and-static-variables-in-a-shared-library-when-it-is-dynam btw, variables in C is extern by default. – Jason Hu Sep 19 '14 at 16:52
  • From the above link;" Again, ODR applies across the board: an extern global variable will be shared across modules, meaning that it should have only one definition across all the modules loaded.". That's my question,I have broken this rule in my scenario,now Wat will happen – Neelansh Mittal Sep 19 '14 at 17:12
  • that means you will get `0` if X references to `a` directly. they are the same variable. – Jason Hu Sep 19 '14 at 17:14
  • X is not accessing directly,it is accessing via function func() – Neelansh Mittal Sep 19 '14 at 17:15
  • where is function x now... any way, they are the same. they are the same variable having the same name located in the same address. no matter it's static or dynamic linking, they will be the same variable eventually. – Jason Hu Sep 19 '14 at 17:17