Thread View: comp.unix.programmer
9 messages
9 total messages
Started by Yves Dufournaud
Mon, 01 Feb 1999 00:00
C++ globals and shared lib, how to ?
Author: Yves Dufournaud
Date: Mon, 01 Feb 1999 00:00
Date: Mon, 01 Feb 1999 00:00
25 lines
1217 bytes
1217 bytes
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 --
Re: C++ globals and shared lib, how to ?
Author: Frank Pilhofer
Date: Mon, 01 Feb 1999 00:00
Date: Mon, 01 Feb 1999 00:00
31 lines
1606 bytes
1606 bytes
In article <36B5EA0E.2361@imag.fr>, Yves Dufournaud wrote: > > I'am loading a file dynamic.o using dlopen > this file is c++ with global object inside. > but when I load it with a dlopen and using RTLD_NOW > constructor of global object are not called. > They should be. Global constructors are called from special initialization hooks in an ELF shared library. > > I've compiled it using > g++ -shared dynamic.cc -export-dynamic > Please try g++ -fPIC -c dynamic.cc g++ -fPIC -o dynamic.so dynamic.o This works for me. You also should upgrade your compiler. Gcc 2.7.2 has some serious drawbacks when building shared libraries. Try gcc 2.8.x or egcs 1.1.x. Frank -- + Frank Pilhofer fp@informatik.uni-frankfurt.de + | http://www.uni-frankfurt.de/~fp/ | +---- Life would be a very great deal less weird without you. - DA ----+
Re: C++ globals and shared lib, how to ?
Author: mgogala
Date: Wed, 03 Feb 1999 00:00
Date: Wed, 03 Feb 1999 00:00
41 lines
2263 bytes
2263 bytes
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 -----------== Posted via Deja News, The Discussion Network ==---------- http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
Re: C++ globals and shared lib, how to ?
Author: Steve no underscores
Date: Wed, 03 Feb 1999 00:00
Date: Wed, 03 Feb 1999 00:00
13 lines
1252 bytes
1252 bytes
Yves Dufournaud <Yves.Dufournaud@imag.fr> writes: >I'am trying basic things using dlopen with g++-2.7.2.3 on sunos 5.5 : >but when I load it with a dlopen and using RTLD_NOW >constructor of global object are not called. Isn't this a known bug with sunos? You could always find the initialisation routine and call it manually I suppose. Steve. -- Stephen Kennedy -- Stephen.Kennedy@cs.tcd.ie
Re: C++ globals and shared lib, how to ?
Author: Casper H.S. Dik - Network Securi...
Date: Wed, 03 Feb 1999 00:00
Date: Wed, 03 Feb 1999 00:00
26 lines
1640 bytes
1640 bytes
[[ PLEASE DON'T SEND ME EMAIL COPIES OF POSTINGS ]] Steve<_s_t_e_v_e_@maths.tcd.ie> no underscores writes: >Yves Dufournaud <Yves.Dufournaud@imag.fr> writes: >>I'am trying basic things using dlopen with g++-2.7.2.3 on sunos 5.5 : >>but when I load it with a dlopen and using RTLD_NOW >>constructor of global object are not called. >Isn't this a known bug with sunos? You could always >find the initialisation routine and call it manually >I suppose. No, this works fine in Solaris 2.x (SunOS 5.x), which is what he is using. You need to link with the compiler and probably with a newer version of gcc/g++ too. SunOS 4.x didn't implement init/fini routines, true but irrelevant Casper -- Expressed in this posting are my opinions. They are in no way related to opinions held by my employer, Sun Microsystems. Statements on Sun products included here are not gospel and may be fiction rather than truth.
Re: C++ globals and shared lib, how to ?
Author: Yves Dufournaud
Date: Wed, 03 Feb 1999 00:00
Date: Wed, 03 Feb 1999 00:00
47 lines
2297 bytes
2297 bytes
Casper H.S. Dik - Network Security Engineer wrote: > > [[ PLEASE DON'T SEND ME EMAIL COPIES OF POSTINGS ]] > > Steve<_s_t_e_v_e_@maths.tcd.ie> no underscores writes: > > >Yves Dufournaud <Yves.Dufournaud@imag.fr> writes: > > >>I'am trying basic things using dlopen with g++-2.7.2.3 on sunos 5.5 : > >>but when I load it with a dlopen and using RTLD_NOW > >>constructor of global object are not called. > > >Isn't this a known bug with sunos? You could always > >find the initialisation routine and call it manually > >I suppose. > > No, this works fine in Solaris 2.x (SunOS 5.x), which is what he is using. > You need to link with the compiler and probably with a newer version of > gcc/g++ too. I've followed the advice that many of you gave me : that is trying to use a newer version of g++. It seems that using the new version ( I've tried both g++-2.8.1 and egcs-?) I can't compile my code due to unresolved reference : In this case, the unresolved reference is a global symbol which must stay in the caller code (the one which do the dlopen call) I see no reason why it couldn't be done, but then how can I build the shared object without resolving that reference at compile time. I've tried the -Ur flag, but without much success. When I do that I get many unresolved reference comming from the standard lib, and their is no object file produced. Cheers, Yves -- Yves Dufournaud Email Yves.Dufournaud at imag.fr equipe MOVI laboratoire GRAVIR --
Re: C++ globals and shared lib, how to ?
Author: Yves Dufournaud
Date: Thu, 04 Feb 1999 00:00
Date: Thu, 04 Feb 1999 00:00
48 lines
2214 bytes
2214 bytes
Yves Dufournaud wrote: > > Casper H.S. Dik - Network Security Engineer wrote: > > > > [[ PLEASE DON'T SEND ME EMAIL COPIES OF POSTINGS ]] > > > > Steve<_s_t_e_v_e_@maths.tcd.ie> no underscores writes: > > > > >Yves Dufournaud <Yves.Dufournaud@imag.fr> writes: > > > > >>I'am trying basic things using dlopen with g++-2.7.2.3 on sunos 5.5 : > > >>but when I load it with a dlopen and using RTLD_NOW > > >>constructor of global object are not called. > > > > >Isn't this a known bug with sunos? You could always > > >find the initialisation routine and call it manually > > >I suppose. > > > > No, this works fine in Solaris 2.x (SunOS 5.x), which is what he is using. > > You need to link with the compiler and probably with a newer version of > > gcc/g++ too. > > I've followed the advice that many of you gave me : that is trying > to use a newer version of g++. OK, I've managed to do what I wanted : You are right there is a bug in g++-2.7.x and that why I got this behavior. Now it compiles fine using g++-2.8.1 (I haven't try egcs). The problem I was having with other globals not resolved, were solved using the -Ur flag for compilation in conjunction with RTLD_GLOBAL in the dlopen function. So it works fine now, thank you for your help. Cheers, Yves. -- Yves Dufournaud Email Yves.Dufournaud at imag.fr equipe MOVI laboratoire GRAVIR --
Re: C++ globals and shared lib, how to ?
Author: Dan Miner
Date: Sat, 06 Feb 1999 00:00
Date: Sat, 06 Feb 1999 00:00
80 lines
3597 bytes
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
Re: C++ globals and shared lib, how to ?
Author: mgogala
Date: Mon, 08 Feb 1999 00:00
Date: Mon, 08 Feb 1999 00:00
92 lines
3829 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads