How to share global data in a share library?
Wed Dec 17 01:20:00 GMT 2003
I wrote a shared library and I know the code of the
shared library will be shared between different
applications. However, the global data of the library
cannot be shared.
For example, the code of the shared library looks
int global = 5;
void setGlobal(int g)
global = g;
I used the following command to create the library
$gcc -fPIC -shared -o libmyshare.so myshare.c
The user application 1 links with it
extern void setGlobal(int);
I used the following command to create u1
$gcc -o u1 u1.c -L. -lmyshare
The user application 2 links with it too
extern void printGlobal(int);
I used the following command to create u2
$gcc -o u2 u2.c -L. -lmyshare
Then, I run u2 and u1
It seems that from u1, setGlobal(50) does not affect
print out of u2, which means, when u1 and u2 link with
the shared library libmyshare.so, there are two
of the global data in libmyshare.so. So the code is
but not the global data.
My question is: is there any way to share the global
within a shared library, i.e, both code and data can
(I know I can use shared memory, but I wonder if we
do it using special compiler settings.)
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
More information about the Gcc-help