This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Antwort: Re: Constructors and destructors of static variables


Dietmar.Kuehl@dvg.de wrote:

> Hi,
>
> Nick Ing-Simmons (nik@tiuk.ti.com) wrote:
> > Adrian Trapletti <Adrian.Trapletti@wu-wien.ac.at> writes:
> > >While compilation, shared linkage and execution yield:
> > >45: g++ -c X.cc
> > >46: ld -shared -o X.so X.o
> > >47: g++ -o main main.cc X.so
> > >48: main
> > >constructor X
> > >destructor X
> > >
> > >Is that what we want, or is this a bug? What about y,
>
> ld is not aware of C++ at all. Thus, it does nothing to construct or
> destruct
> objects in the resulting object. I don't know whether it is possible to
> link
> shared libraries with g++ and whether this would arrange for the
> constructors
> to be called. In any case, loading of shared objects on UNIX systems
> normally
> results in calling symbols like "_init" (see the documentation of dlopen()
> for
> more information an the exact name) which can be used to construct the
> objects.
>

It is in fact very simple, just use

g++ -shared -o X.so X.o

instead and the constr. and destr. are called as desired.
Even if X.so is used by some high level Mathprogram, such as
R, which is written in plain C, the constr. and destr. are called as desired.

Thanks for your help

Adrian

>
> g++ allows the specification of attributes (see the documentation of
> __ATTRIBUTE__) which in turn can be used to place symbols into a certain
> section in the object file. It might be possible to use this feature to
> arrange for proper construction. However, while I have used the other
> approach
> (but only for object explicitly loaded with dlopen()) I haven't tried this
> approach yet.
>
> > BEWARE - you are using cout which is itself a static object.
> > It is quite possible that Y::Y() is being called before cout
> > has been constructed (the C++ IO is not linked to your X.so).
>
> Although it is possible, I doubt that this is the problem: Using cout prior
> to
> construction would probably result in fatal program errors, probably in a
> core
> dump. In addition, the standard says that cout is constructed prior to any
> use
> anyway. On the other hand, what the standard says does not really apply to
> the
> IOStream library shipped with gcc and this library may be buggy...
>
> dk

--
Adrian Trapletti, Vienna University of Economics and Business
Administration, Augasse 2-6, A-1090 Vienna, Austria
Phone: ++43 1 31336 4561, Fax: ++43 1 31336 708,
Email: adrian.trapletti@wu-wien.ac.at





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]