help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Instanciation of global variables in shared library : recent changes


From: Helfer Thomas
Subject: Re: Instanciation of global variables in shared library : recent changes ?
Date: Fri, 18 May 2012 08:41:44 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]