Static variable sharing in shared libraries
Hello,
I have 2 shared libraries(slave and common) and one binary executable(host). host uses slave and both host and slave use common. Code:
[host]->[slave] The problem is that when i call a method from slave that calls setCommonValue from common and now i call getCommonValue(in host) the value set in slave is returned. 1) why? 2) how could i link so that this problem does not arise? The sample output. I expect 1000 and 1000 Code:
$ ./host Code:
$ldd host Code:
$ldd libslave.so Code:
$ nm -g libslave.so | grep ' T ' Code:
$ldd libcommon.so make case1 builds my sample. Code:
//common.h Code:
//common.cpp Code:
//slave.h Code:
//slave.cpp Code:
//host.cpp Code:
#Makefile |
i dont understand why you are expecting 1000, 1000 when clearly you are setting the value to 1000, and then 500. ??
Code:
const char * doSlaveStuff(){ |
Hi -
I think maybe you have the wrong expectations. A variable is just a variable: it doesn't matter if it's from a shared library (.so), a static library (.a) or a static object (.o) - it's exactly the same in every case. Here are a couple of links that might explain a bit better: http://en.wikipedia.org/wiki/Library_(computer_science) http://www.iecc.com/linker/linker10.html SUGGESTION: What's the problem you're really trying to solve? When you said "problem" above, you were actually restating "what" you were doing, not the "why". If we knew the "requirement", the actual goal, then perhaps we could suggest some alternatives for you. |
Yes, it's the same common variable. Make a "cookie" :) containing the data specific to each of common's "clients" and get the client to look after it! I mean a struct, of course.
--------- Common: typedef COMMON_COOKIE {...} --------- Client such as slave or master: COMMON_COOKIE my_common; set_common_value(my_common, 27); ---------- |
Quote:
|
Quote:
|
Quote:
|
I read about -Bsymbolic option in gcc, it seemed thats what i need, but no effect whatsoever. any ideas?
|
In this case are there duplicate symbols? One get/setCommonValue in common and one in slave(when linked staticly with common).
I did a experiment: Code:
#include "common.h" |
Well i have solved the problem by implementing a version script. So...
Code:
$ cat slave.v Code:
gcc -Wall -fPIC -c common.cpp -o common.o Code:
$ ./host Cheers! |
interesting... what is the version script doing?
|
ristiisa -
I strongly suspect that whatever you *think* you got working is probably more by coincidence than by design. I still think you've got some fundamental misconceptions about how processes can access and share global data with each other, and about what shared libraries can (or cannot!) do for you. Please do glance at these links (if you haven't already): http://en.wikipedia.org/wiki/Shared_libraries http://www.iecc.com/linker/linker10.html http://people.redhat.com/drepper/dsohowto.pdf IMHO .. PSM |
I was gonna say make them static or just pass the value to the functions and not use a golbal variable. Then again this is C++ witch I stay away from :p
|
Quote:
|
All times are GMT -5. The time now is 04:18 AM. |