Re: Shared libraries: How to share global variaables?
Jan-Benedict Glaw <[email protected]> Sat, 30 Oct 2004 16:45:23 +0200
| Newsgroups | org.kernel.vger.linux-gcc |
|---|---|
| Message-ID | <[email protected]> |
On Sat, 2004-10-30 14:50:11 +0200, Robin Doer <[email protected]> wrote in message <[email protected]>: > 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 [email protected] . +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));
signature.asc
(application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) iD8DBQFBg6kDHb1edYOZ4bsRAnsAAJ48CfEpN16FEjG57vfboM7yDixZagCgj6bn a5ublF0WiXvkkORU+OnQGaY= =0hiT -----END PGP SIGNATURE-----