Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
call init function when *loading* dynamic shared libraries?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

call init function when *loading* dynamic shared libraries?



Hi,

I'm programming in C++ for Mac OS X 10.2.2. I'm using December 2002 Mac
OS X Developer Tools.

I would like the constructors of static instances of classes to be
called automatically when a dynamic shared library is *loaded* into a
program. Not when a symbol is used from the library. I can't get it to work.

Actually I've even tried with a simple C program and a function
specified with the -init option flag. This function is not called upon
loading the dynamic shared library - only when the first symbol is
used from the library.

The -bind_at_load option flag doesn't help either.




Here is the source code of the libfoo.dylib shared dynamic library:

	#include <stdio.h>

	void library_init() {
	  printf("library_init()\n");
	}

	void use_library_symbol() {
	  printf("use_library_symbol()\n");
	}


Here is how I build it: gcc -o libfoo.dylib -dynamiclib -bind_at_load \ -init _library_init foo.c


Here is the source of the a.out main program:

	extern void use_library_symbol();

	int main() {
	  /* use_library_symbol(); */
	  return 0;
	}


Here is how I build it: gcc main.c -L. -lfoo


Here is what happens when I run it: $ ./a.out $


Now if I uncomment the commented line so that a library symbol is used: $ ./a.out library_init() use_library_symbol() $




Is there any way to get intialization routines called when *loading* a dynamic shared library? Am I maybe doing anything wrong when linking the library or the program?

Regards,
--
Dimitri
_______________________________________________
unix-porting mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/unix-porting
Do not post admin requests to the list. They will be ignored.





Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.