opensubscriber
   Find in this group all groups
 
Unknown more information…

h : help-gplusplus@gnu.org 18 May 2012 • 2:41PM -0400

Re: Instanciation of global variables in shared library : recent changes ?
by Helfer Thomas

REPLY TO AUTHOR
 
REPLY TO GROUP




Thanks a lot, it worked fine

Le 17/05/2012 23:36, Jan Seiffert a écrit :
> Helfer Thomas schrieb:
>> Hi,
>>
>> I already posted this question on fr.comp.lang.c++ without response.
>>
>> I am experiencing some trouble in the instatiation of global
>> variables in recent ubuntu versions. I don't know if this question is
>> directly related to g++ or the linker as the following example works
>> fine on debian squeeze with gcc 4.7 and fails on the two last
>> versions of ubuntu. If the question is not relevant in this mailing
>> list, please tell me where I shall redirect it.
>>
>> I now show a simple code snipset to illustrate the trouble.
>>
>> Consider the following code in file A.cxx :
>>
>> #include<iostream>  struct A { A(){ std::cout<<  "A"<<  std::endl; }
>> };
>>
>> A a; // global variable !!
>>
>> This file is used to produce a shared library libA.so : g++ --shared
>> -fPIC A.cxx -o libA.so
>>
>> Consider now an empty main in file test.cxx: #include<cstdlib>  int
>> main(void){return EXIT_SUCCESS;}
>>
>> This file is turned into an executable which is linked with the
>> previous library: g++ test.cxx -o test -L. -lA
>>
>> on debian squeeze (an all previous versions of linux) : this prints
>> "A" du to the constructor of the global variable on ubuntu linux
>> precise pangolin, nothing is printed !
>>
>> This is really annoying and none of __attribute__ I tried solved the
>> problem. This is also strange to see that if the libA is dlopened,
>> the program displays the expected message ("A")...
>>
>> Could anyone tell me : 1) what has changed 2) if the previous
>> behaviour can be retrieved
>>
>
> This is just a hunch, but i guess what you are seeing is the linker option
> "--as-needed". On Ubuntu (and propably a lot of other modern Distros) it is
> set by default to reduce cross linking and dependencies and app start time.
> On Debian it looks like it is not set by default.
>
> There is no linking dependency between your main file and the lib.
> With --as-needed the linker discards the supplied "-lA", because your program
> does not seem to use it.
> You can check the list of used libs with tools like ldd or objdump.
>
> To remedy the situation you either have to import some symbol from libA
> or you have to switch between "--no-as-needed" and back to "--as-needed",
> like so:
> g++ test.cxx -o test -L . -Wl,--no-as-needed -lA -Wl,--as-needed
>
>> Thanks for any help
>
> Greetings
> Jan

_______________________________________________
help-gplusplus mailing list
help-gplusplus@gnu....
https://lists.gnu.org/mailman/listinfo/help-gplusplus

Bookmark with:

Delicious   Digg   reddit   Facebook   StumbleUpon

Related Messages

opensubscriber is not affiliated with the authors of this message nor responsible for its content.