* Re: Shared libraries: How to share global variaables?
2004-10-30 12:50 ` Robin Doer
@ 2004-10-30 13:13 ` Naga Raju
2004-10-30 14:45 ` Jan-Benedict Glaw
1 sibling, 0 replies; 4+ messages in thread
From: Naga Raju @ 2004-10-30 13:13 UTC (permalink / raw)
To: linux-gcc
Hi Robin,
Thanks for the help.
But my problem is little different.
In the example you have given, I initialized
char foo[100]="";
and in one application I set
application 1:
extern char foo[100];
strcpy(foo,"Test message");
printf("%s",foo);
and in applications 2:
extern char foo[100];
printf("%s",foo);
But application1 disaplays
Test message.
application2:
(No output ..it prints '\0' )
I want application2 to display "Test message"
Why the variables in two applications are different?
Why are they not using the same global variable?
Regards,
Nagaraju
On Sat, 30 Oct 2004 14:50:11 +0200, Robin Doer <robin@robind.de> wrote:
> Am Samstag, 30. Oktober 2004 07:21 schrieb Naga Raju:
>
>
> > Is it possible to share global variables such that all applications
> > which use shared libraries can see the changes made to the global
> > variables by the other applications.
> >
> > I use gcc and compiled
> > gcc -shared -Wl,-soname,xyz.so.1 -o libxyz.so.1.0 -lxyz2 abc.o
> >
>
> Well, imho you can use the "extern" keyword.
>
> See the following example:
>
> bash-2.05b$ cat foo.c
> const char* foo = "Hello World"; /* Global variable foo */
>
> bash-2.05b$ cat bar.c
> #include <stdio.h>
>
> extern const char* foo;
>
> int main(int argc, char* argv[]) {
> printf("%s\n", foo);
>
> return 0;
> }
>
> bash-2.05b$ gcc -shared -Wl,-soname,libfoo.so -o libfoo.so foo.c
> bash-2.05b$ gcc -Wall bar.c -L. -lfoo -o bar
> bash-2.05b$ ./bar
> Hello World
>
> > Regards,
> > Nagaraju.
>
> Bye,
> Robin
>
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: Shared libraries: How to share global variaables?
2004-10-30 12:50 ` Robin Doer
2004-10-30 13:13 ` Naga Raju
@ 2004-10-30 14:45 ` Jan-Benedict Glaw
1 sibling, 0 replies; 4+ messages in thread
From: Jan-Benedict Glaw @ 2004-10-30 14:45 UTC (permalink / raw)
To: linux-gcc
[-- Attachment #1: Type: text/plain, Size: 1766 bytes --]
On Sat, 2004-10-30 14:50:11 +0200, Robin Doer <robin@robind.de>
wrote in message <200410301450.15154.robin@robind.de>:
> Am Samstag, 30. Oktober 2004 07:21 schrieb Naga Raju:
> > Is it possible to share global variables such that all applications
> > which use shared libraries can see the changes made to the global
> > variables by the other applications.
>
> Well, imho you can use the "extern" keyword.
> See the following example:
> bash-2.05b$ cat foo.c
> const char* foo = "Hello World"; /* Global variable foo */
>
> bash-2.05b$ cat bar.c
> #include <stdio.h>
>
> extern const char* foo;
>
> int main(int argc, char* argv[]) {
> printf("%s\n", foo);
>
> return 0;
> }
>
> bash-2.05b$ gcc -shared -Wl,-soname,libfoo.so -o libfoo.so foo.c
> bash-2.05b$ gcc -Wall bar.c -L. -lfoo -o bar
> bash-2.05b$ ./bar
> Hello World
As stated, the initial author had something different in mind: to have
two programs linking one common shared library in a way that one program
(in it's VM space) changes a (common to both programs) variable and
automagically, this variable also changes in the 2nd program.
Actually, early Windows versions had something like that.
It's possibly to achieve this by some hacks, but I won't tell you how to
do that--it's evil.
Just use shared memory; asking Google for "linux programming shared
memory" will give you some examples and book tips.
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread