Hiya all.
I've posted this on comp.os.linux.development.apps but got no response
so I thought I might have more luck here (even though I'm not strictly
speaking working on the system).
I have a shared library that depends on another shared library. In
other OSs (Solaris HP-UX) this causes the global variables in the
second library to be initialized first but in Linux I see that the
order of initialization is wholly dependant on the order of the
libraries on the link line (they get initialized in reverse order to
that of the link line). In AIX it's possible to give a shared library
a priority at link time (-qmkshrobj[=priority] ) which is much uglier
but at least you don't have to worry about the order on link line.
Is there any way to get g++ to choose the correct initialization
order?
Here is a sample:
----- used.C
class multiplier {
int i;
public:
operator int() const { return i; }
multiplier(int ii, int jj) : i(ii*jj) { }
multiplier obj(7,6);Quote:};
int usedFunc()
{
return obj;
----- user.CQuote:}
int usedFunc();
int userInt = usedFunc();
int userFunc()
{
return userInt;
----- main.CQuote:}
int userFunc();
int main()
{
return userFunc() -42;
----- Makefile (I was lazy so it only works on Linux and SolarisQuote:}
ifeq ($(OSTYPE),linux)
LD=g++ --shared
CXX=g++ -g
else
# solaris
LD=CC -G
CXX=CC -g
endif
all: libuser.so libused.so
${CXX} main.C -L. -lused -luser -o runme
libuser.so: user.o libused.so
libused.so: used.o
used.o: used.C
user.o: user.C
clean:
-/bin/rm -rf *.o *.so core runme
------
After I run gmake I checked with ldd to make sure that libuser.so is
dependant on libused.so.
In Solaris this is enough and when I run ./runme the global object in
libused.so get initialized before those of libuser.so and the return
code is the expected 0. But in linux this doesn't happen and runme
fails (returns a nonzero value).
Any ideas?
Also if you think there is another group which may be able to help me
on this please point it out.
P.S. Yes I know I could put the libraries in the correct order but
where's the fun in that? also in real life we're talking about
something slightly more complex than my sample....
--
If at first you don't succeed, post, post, post again.