🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #3386

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

#3386
From: Dan Miner
Date: Sat, 06 Feb 1999 00:00
80 lines
3597 bytes
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
Technical Details
Message-ID: <918255234.345483@iris.nyx.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <36B5EA0E.2361@imag.fr> <798act$a7i$1@nnrp1.dejanews.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fc89c,3b56ba1236b2e4f
X-Google-Attributes: gidfc89c,public
X-Google-Thread: 11035d,3b56ba1236b2e4f
X-Google-Attributes: gid11035d,public
X-Google-Thread: 10ba33,3b56ba1236b2e4f
X-Google-Attributes: gid10ba33,public
From: Dan Miner <dminer@nyx.net>
Subject: Re: C++ globals and shared lib, how to ?
Date: 1999/02/06
Message-ID: <918255234.345483@iris.nyx.net>
X-Deja-AN: 441253969
X-Disclaimer: Nyx is a Free Public Access Internet Service: http://www.nyx.net Nyx is not responsible for the actions of its users. Our AUP / Free Speech Policy are at http://www.nyx.net/policies/ Direct complaints to abuse@nyx.net
References: <36B5EA0E.2361@imag.fr> <798act$a7i$1@nnrp1.dejanews.com>
X-Post-Path: iris.nyx.net!dminer@nyx10.nyx.net
X-Trace: wormhole.dimensional.com 918276834 206.124.29.7 (Fri, 05 Feb 1999 21:53:54 MDT)
Organization: Nyx Net, Free Internet access (www.nyx.net)
User-Agent: tin/pre-1.4-980105 (UNIX) (SunOS/4.1.3 (sun4m))
NNTP-Posting-Date: Fri, 05 Feb 1999 21:53:54 MDT
Newsgroups: comp.unix.programmer,comp.unix.solaris,gnu.g++.help