ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ?
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).
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.
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.
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.