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.
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]
\ /
[common]
In common there is a static variable that can be set/get through commons setCommonValue/getCommonValue functions. I link slave statically with common and host shared with slave and common.
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
1000
Slave acts
500
Code:
$ldd host
libslave.so => not found
libcommon.so => not found
libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x40027000)
libc.so.6 => /lib/tls/libc.so.6 (0x400e1000)
libm.so.6 => /lib/tls/libm.so.6 (0x40216000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x40238000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
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(){
setCommonValue(500);
return "Slave acts";
}
...
int main(){
setCommonValue(1000); // <-- you are setting 1000 here
cout << getCommonValue() << endl;
cout << doSlaveStuff() << endl; // <-- you are setting it to 500 here (see above)
cout << getCommonValue() << endl;
return 0;
}
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:
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.
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(){
setCommonValue(500);
return "Slave acts";
}
...
int main(){
setCommonValue(1000); // <-- you are setting 1000 here
cout << getCommonValue() << endl;
cout << doSlaveStuff() << endl; // <-- you are setting it to 500 here (see above)
cout << getCommonValue() << endl;
return 0;
}
or am i missing something?
Host uses common as a shared library, but slave uses common in a static library. I thought the memory spaces would not overlap.
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:
...
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.
Well the problem is how to use common so that the variable would stay unchanged in host if slave changes it and vice versa? Consider the common to be a third-party library that both host and slave would like to use simultaneously.
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);
----------
In this case changing the common is not an option. But good suggestion.
I compiled common with -DUSEVAL1 and then compiled slave and linked them. Then i compiled common winthout USEVAL1 into shared library. But no success...
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):
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):
The real life problem was that i had a application using Qt and a shared library using Qt. Both designed so that using them together resulted conflict in sharing Qt's global variables(like db connections and text codecs). The reason was in the design of that shared library, designed to be cross platform but forgot to take notice about shared library behavior under linux. Perhaps the solution i found was a mere coincidence or a loophole but on windows dll's act like that in the first place. You have to go extra trouble for wanting to export your symbols and to import foreign exported symbols. If i compile the same project under windows with gcc and with ms compiler then gcc exports everything and cl exports nothing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.