This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
How to share global data in a share library?
- From: Song Wang <wsonguci at yahoo dot com>
- To: gcc-help at gcc dot gnu dot org, gcc at gcc dot gnu dot org
- Cc: wsonguci at yahoo dot com
- Date: Tue, 16 Dec 2003 17:20:55 -0800 (PST)
- Subject: How to share global data in a share library?
Hi,
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
like
// myshare.c
#include <stdio.h>
int global = 5;
void printGlobal()
{
printf("Global=%d\n",global);
}
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
//u1.c
#include <stdio.h>
extern void setGlobal(int);
int main()
{
setGlobal(50);
return 0;
}
I used the following command to create u1
$gcc -o u1 u1.c -L. -lmyshare
The user application 2 links with it too
//u2.c
#include <stdio.h>
extern void printGlobal(int);
int main()
{
printGlobal();
return 0;
}
I used the following command to create u2
$gcc -o u2 u2.c -L. -lmyshare
Then, I run u2 and u1
$u2
Global=5
$u1
$u2
Global=5
It seems that from u1, setGlobal(50) does not affect
the
print out of u2, which means, when u1 and u2 link with
the shared library libmyshare.so, there are two
copies
of the global data in libmyshare.so. So the code is
shared,
but not the global data.
My question is: is there any way to share the global
data
within a shared library, i.e, both code and data can
be shared?
(I know I can use shared memory, but I wonder if we
can
do it using special compiler settings.)
Thanks.
-Song
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/