linux-gcc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jan-Benedict Glaw <jbglaw@lug-owl.de>
To: linux-gcc@vger.kernel.org
Subject: Re: Shared libraries: How to share global variaables?
Date: Sat, 30 Oct 2004 16:45:23 +0200	[thread overview]
Message-ID: <20041030144523.GC11105@lug-owl.de> (raw)
In-Reply-To: <200410301450.15154.robin@robind.de>

[-- 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 --]

      parent reply	other threads:[~2004-10-30 14:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <S263586AbUJ3FQs/20041030051648Z+543@vger.kernel.org>
     [not found] ` <e9ddb88e04102922207ce4eb04@mail.gmail.com>
2004-10-30  5:21   ` Shared libraries: How to share global variaables? Naga Raju
2004-10-30 12:50     ` Robin Doer
2004-10-30 13:13       ` Naga Raju
2004-10-30 14:45       ` Jan-Benedict Glaw [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20041030144523.GC11105@lug-owl.de \
    --to=jbglaw@lug-owl.de \
    --cc=linux-gcc@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).