LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-21-2008, 09:56 AM   #1
arvind.ayyangar
LQ Newbie
 
Registered: Aug 2006
Posts: 17

Rep: Reputation: 0
shared data in shared libraries


Hi all,
How can I have a shared variable in a shared library, which would be the only copy of the variable for the entire process.
To give an e.g of what I want to achieve, I have a process P which links to libraries S1 and S2. S1 also depends on S2. A variable in S2 is to be used by both S1 and P, and I need a single copy instead.
How can I do this ?


--
Cheers
Arvind
 
Old 10-22-2008, 07:00 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
The only way to share data between two difference processes is to use shared memory.

The APIs you're interested in include:

mmap ()
... or ..
shmat ()

Using "shared memory" has *nothing* to do one way or the other with using "shared libraries" (although Windows .dll implementation confuses the issue somewhat).

Here's a good tutorial:
http://fscked.org/writings/SHM/shm.html
<= Note: Linux supports both the BSD and SysV styles
 
Old 10-22-2008, 11:09 PM   #3
arvind.ayyangar
LQ Newbie
 
Registered: Aug 2006
Posts: 17

Original Poster
Rep: Reputation: 0
I dont think you understood my question correctly. I have not mentioned that the data is to be shared between two different processes.

AFAIK, variables (global) in a shared library have just one copy even though they may be used by different libraries of the same process. I was just a bit confused about this when I read about some of the gcc attributes for variables.


Thanks anyways..
 
Old 10-23-2008, 12:16 AM   #4
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
I presume we are talking about C or C++ here. In this case, there generally will be an include file that goes with the library, and that include file will have variable declarations in it. For your variable to be shared, it would be an extern declaration in the include file, and would match an exported symbol in the library.
 
Old 10-23-2008, 01:01 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
It's all about linking! I'd put the variable in S2, build S2 first, build S1 and link it to S2, then build P and link it to both S1 and S2. That should work! As long as two binaries (library or executable) are linked to the same library, only one copy of its contents should be present for both to use, provided both binaries are themselves linked to each other.
ta0kira

PS An example:
Code:
//library1.h

extern int value;
Code:
//library1.c

#include "library1.h"

int value = 0;
Code:
//library2.h

extern void change_value();
Code:
//library2.c

#include "library2.h"
#include "library1.h"

void change_value()
{ value = 1; }
Code:
//main.c

#include <stdio.h>
#include "library1.h"
#include "library2.h"

int main()
{
    printf("before: %i\n", value);
    change_value();
    printf("after:  %i\n", value);
}
Code:
> gcc library1.c -shared -o library1.so
> gcc library2.c -shared -o library2.so ./library1.so
> gcc main.c -o testmain ./library1.so ./library2.so
> ./testmain
before: 0
after:  1

Last edited by ta0kira; 10-23-2008 at 01:12 AM.
 
Old 10-23-2008, 01:15 AM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 600

Rep: Reputation: 126Reputation: 126
Also look at:

http://www.redhat.com/magazine/011sep05/features/gcc/

which gives some hints about gcc and flags that can aid the right "aliasing" (to handle globals).

This was got by googling "gcc+globals"
 
Old 10-23-2008, 10:11 AM   #7
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
arvind.ayyangar -

Quote:
AFAIK, variables (global) in a shared library have just one copy even though they may be used by different libraries of the same process.
This is absolutely not correct. Whether or not a variable is "global" has everything to do with the way you've declared it in C/C++ (global scope, "extern", etc.) and *nothing* to do with whether or not you happened to allocate it in a shared library, a static library or simply in one or another .o file.
 
Old 10-23-2008, 03:53 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by AnanthaP View Post
Also look at:

http://www.redhat.com/magazine/011sep05/features/gcc/

which gives some hints about gcc and flags that can aid the right "aliasing" (to handle globals).

This was got by googling "gcc+globals"
I don't think aliasing matters since, as far as I know, extern globals are never optimized out because you explicitly state that they might be used externally. Or maybe you're talking about __attribute__ ((alias("...")))?
ta0kira
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error while loading shared libraries: libstdc++.so.5: cannot open shared object file: Franziss Linux - Newbie 10 06-28-2010 05:47 AM
Urgent !!! rpm: error while loading shared libraries: libelf.so.1: cannot open shared tinaa Linux - Software 5 12-02-2008 03:19 PM
error while loading shared libraries: libstdc++.so.5: cannot open shared object file PaulyWally Debian 2 10-18-2008 05:59 PM
error while loading shared libraries: libgvc.so.3: cannot open shared object file coolrock Slackware 6 01-17-2007 05:10 PM
error while loading shared libraries: libdb-4.1.so: cannot open shared object file putquery8581 Linux - Software 1 10-01-2004 07:03 AM


All times are GMT -5. The time now is 10:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration