RE: dlopen() on 10.3 and static initializers: SOLVED
RE: dlopen() on 10.3 and static initializers: SOLVED
- Subject: RE: dlopen() on 10.3 and static initializers: SOLVED
- From: "David Litwin" <email@hidden>
- Date: Thu, 6 Apr 2006 15:20:01 -0700
- Thread-topic: dlopen() on 10.3 and static initializers: SOLVED
As it happens __attribute__((constructor)) makes the _init() call run in
10.4, but does nothing to help 10.3.
One thing that got it working for me was to have all my calls to
dlopen() be followed by the following:
InitFunc initLoadedLib = (InitFunc) dlsym(dlHandle, "_Z5_initv");
initLoadedLib();
This, of course, led me to realize that _init() had to be extern "C" for
the dlcompat code to find it :)
Dave
-----Original Message-----
From: David Litwin
Sent: Thursday, April 06, 2006 1:39 PM
To: Peter O'Gorman
Cc: Xcode Users
Subject: RE: dlopen() on 10.3 and static initializers
It looks like converting to bundles won't help either. My loadable
libraries depend on each other as well as some dynamic libs not
dlopened(). When I try to link against them, I get:
<foo>.bundle is input for the dynamic link editor, is not
relocatable by the static link editor again
For those who might want to convert a .dylib to a .bundle, here is what
I learned you have to do:
1.) Unset the "Current Library Version" in the project settings. It
will look unset, but it is lying. Double click on it and delete the
"$(inherited)"
2.) Unset the "Compatibility Version" in the project settings. Same as
1.) above.
3.) Unset the "Current Library Version" in the target settings. For me,
this was set at the Target level to "1".
4.) Unset the "Compatibility Version" in the target settings. Same as
3.) above.
5.) Change the "Mach-O Type" in the target settings from "Dynamic
Library" to "Bundle"
6.) Check the "Perform Single-Object PreLink" checkbox in the target
settings. Without this, you will get -single_module defined on your
link line and that only works with the -dylib option, which you lost
when you changed the Mach-O Type to bundle.
7.) Change the "Executable Extension" of the target to "bundle" from
"dylib"
I suppose my next move is to try adding __attribute__((constructor)) to
the _init() call, but I'm sure if that was necessary it would have been
mentioned...
Dave
-----Original Message-----
From: David Litwin
Sent: Thursday, April 06, 2006 12:09 PM
To: Peter O'Gorman
Cc: Xcode Users
Subject: RE: dlopen() on 10.3 and static initializers
No luck :( The printf in my _init() is not showing up, and I'm not
seeing any of the static initializers either.
My next attempt would be to convert the libs to be bundles, but perhaps
I'm missing something simple.
I first changed the MACH_O_TYPE to MH_BUNDLE, but then found that I had
to change lots of other settings like unsetting compat version (both
project and target), current version (same) and now it's complaining it
can't link with single-module.
I've set none of these myself, apparently they were all set when I chose
the dynamic library template. It's a bit hard to find how to unset them
properly, and I'm beginning to think I'm going about this the wrong way.
I don't want to rebuild all the projects (there are a number of them) is
there a better way?
Dave
-----Original Message-----
From: David Litwin
To: Peter O'Gorman
Cc: Xcode Users
Subject: RE: dlopen() on 10.3 and static initializers
These are dynamic shared libraries, and have the TWOLEVEL flag set when
I run otool -hv.
I noticed a "-Wl,-single_module" on my link line so I believe that to be
already correct, perhaps because I have the LIBRARY_STYLE set to
DYNAMIC.
I'll give the _init trick a try.
Thanks,
David Litwin
BigFix
-----Original Message-----
From: Peter O'Gorman [mailto:email@hidden]
On Wed, 2006-04-05 at 19:12 -0700, David Litwin wrote:
> I'm in the process of qualifying my app on 10.3.
>
>
> My app uses dlopen() to load some libraries, which contain a number of
> static objects that register themselves to the app in their
> constructors.
>
> While this works fine in 10.4, on 10.3 these registrations never
> occur. There are no errors loading the libraries, but it seems that
> no C++ static object constructors are being called.
>
Are you dlopening dynamic shared libraries or bundles? On 10.3 static
constructors were more likely to be called for MH_BUNDLE than MH_DYLIB.
If they are dynamic shared libraries and you can not change that, are
they two level namespace or flat namespace? (hint otool -hv).
If twolevel, try adding this to each library:
void _init(void);
void _init() {;}
And ensure that you link your libraries with -single_module.
dlcompat's dlopen will look up and call _init for each library it
explicitly dlopens, and if the library is linked single_module, all the
module's static initializers will be called at that time.
On 10.4 the dynamic linker was rewritten to work better and dlopen()
became part of the dynamic linker, not an external function that used
dyld.
Hope this helps,
Peter
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden