Re: Shared libraries: How to share global variaables?

Robin Doer <[email protected]> Sat, 30 Oct 2004 14:50:11 +0200
Newsgroups org.kernel.vger.linux-gcc
Message-ID <[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.
>
> 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
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBBg44HwnZkqspVX/oRAn72AJoD/ElUOoffJKOTHXTo3Qj7hwzABgCcDupB
EzSoPbmgvGkuReJiLU/YAnQ=
=TZ57
-----END PGP SIGNATURE-----