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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-21-2008, 09:56 AM
|
#1
|
|
LQ Newbie
Registered: Aug 2006
Posts: 17
Rep:
|
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
|
|
|
|
10-22-2008, 07:00 PM
|
#2
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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
|
|
|
|
10-22-2008, 11:09 PM
|
#3
|
|
LQ Newbie
Registered: Aug 2006
Posts: 17
Original Poster
Rep:
|
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..
|
|
|
|
10-23-2008, 12:16 AM
|
#4
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
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.
|
|
|
|
10-23-2008, 01:01 AM
|
#5
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,967
Rep: 
|
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.
|
|
|
|
10-23-2008, 01:15 AM
|
#6
|
|
Member
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 536
Rep:
|
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"
|
|
|
|
10-23-2008, 10:11 AM
|
#7
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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.
|
|
|
|
10-23-2008, 03:53 PM
|
#8
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,967
Rep: 
|
Quote:
Originally Posted by AnanthaP
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:04 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|