Global variable in a Linux shared library

Posted by user3730495 on Programmers See other posts from Programmers or by user3730495
Published on 2014-06-11T14:52:46Z Indexed on 2014/06/11 15:40 UTC
Read the original article Hit count: 119

Filed under:
|

Suppose we have the following setup under Linux, .so library named "libcnt.so" and 3 user space apps: "app1", "app2", "app3". This library does 1 simple thing, it says to the app (app dynamically links the library at runtime) by how many apps it is already linked. Apps should have access to link counter. My knowledge in C and Linux is somewhat limited in this aspect, but as I understand this information should be stored in a global variable inside the shared object. Something like:

in libcnt.h extern int cnt_loads;

in libcnt.c int cnt_loads = 0; // where each linking increments this counter

or something...

So, my question is how it should be declared and/or defined inside .so library to guaranty that multiple apps from user space get the same instance of that variable counter?

© Programmers or respective owner

Related posts about c++

Related posts about c