🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #3387

Re: C++ globals and shared lib, how to ?

#3387
From: mgogala
Date: Mon, 08 Feb 1999 00:00
92 lines
3829 bytes
In article <918255234.345483@iris.nyx.net>,
  Dan Miner <dminer@nyx.net> wrote:
> In comp.unix.programmer mgogala@yahoo.com wrote:
> > In article <36B5EA0E.2361@imag.fr>,
> >   Yves Dufournaud <Yves.Dufournaud@imag.fr> wrote:
> >> Hi,
> >>
> >> I'am trying basic things using dlopen with g++-2.7.2.3 on sunos 5.5 :
> >>
> >> I'am loading a file dynamic.o using dlopen
> >> this file is c++ with global object inside.
> >>
> >> I've compiled it using
> >> g++ -shared dynamic.cc -export-dynamic
> >>
> >> but when I load it with a dlopen and using RTLD_NOW
> >> constructor of global object are not called.
> >>
> >> I don't understand why (my version of g++ generate elf) ?
> >>
> >> please help,
> >>
> >> thanks,
> >>
> >> Yves.
> >>
> >> --
> >> Yves Dufournaud Email Yves.Dufournaud at imag.fr equipe MOVI laboratoire
> >> GRAVIR
> >> --
> >>
> > Constructors for global objects are called from global initialization
routine
> > typically caled crtbegin.o, which is linked into the ELF file when it
> > contains function "main". If you use dlsym and dlopen, to invoke the
> > routine, your program will not call constructors for the global objects in
> > that shared object. The only way to initialize global objects specified in
> > the shared library is to invoke some kind of explicit creation routine and
> > initialize them manually. That means to include routine named
"InitMyGlobals"
> > in the shared library and then use dlopen to open it and then dlsym to get
> > the symbol you are looking for.
> > Mladen Gogala
>
> Yep.. That's the problem.  You can make the function mentioned (BTW, you'll
> have to extern "C" so dlsym will find it due to C++ name mangling).
>
> There is another option.  I understand in ELF, you have the .init and .fini
> sections which are called automagically on loading or dlopen().  I've played
> with the on Linux and I understand it's an ELF-thing.  :)
>
> It has been a while but I believe this translates into:
>
> extern "C" {
> void init(void);
> void fini(void);
> }
>
> // Create a tradition-like global initializer.
>
> MyClass &obj;
>
> void init(void)
> {
>     MyClass *o = new MyClass();
>
>     ASSERT( o != NULL );
>     obj = &o;
> }
>
> void fini(void)
> {
>     MyClass *o = &obj;
>
>     delete o;
> }
>
> 	Hope this helps,
> 		Dan
> --
>  Dan Miner    dminer@nyx.net |                                        | Doing
>          Programmer/         |                                        | Linux
>       Linux Consultant       | "What yonder light Windows 95 breaks?" | since
>  http://www.nyx.net/~dminer/ |    "Free software: The New Frontier"   | v0.12
>
Now this is a neat trick! I'll try it from Intel Solaris and let you
know if it works. Thanks again!

Mladen Gogala

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    
Technical Details
Message-ID: <79mt1j$r4$1@nnrp1.dejanews.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <36B5EA0E.2361@imag.fr> <798act$a7i$1@nnrp1.dejanews.com> <918255234.345483@iris.nyx.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11035d,3b56ba1236b2e4f
X-Google-Attributes: gid11035d,public
X-Google-Thread: fc89c,3b56ba1236b2e4f
X-Google-Attributes: gidfc89c,public
X-Google-Thread: 10ba33,3b56ba1236b2e4f
X-Google-Attributes: gid10ba33,public
From: mgogala@yahoo.com
Subject: Re: C++ globals and shared lib, how to ?
Date: 1999/02/08
Message-ID: <79mt1j$r4$1@nnrp1.dejanews.com>
X-Deja-AN: 441841864
References: <36B5EA0E.2361@imag.fr> <798act$a7i$1@nnrp1.dejanews.com> <918255234.345483@iris.nyx.net>
X-Http-Proxy: 1.0 x3.dejanews.com:80 (Squid/1.1.22) for client 207.237.12.66
Organization: Deja News - The Leader in Internet Discussion
X-Article-Creation-Date: Mon Feb 08 14:40:53 1999 GMT
Newsgroups: comp.unix.programmer,comp.unix.solaris,gnu.g++.help
X-Http-User-Agent: Mozilla/4.5 [en] (WinNT; I)