Professional Web Applications Themes

Dynamic library initialization - Mac Programming

Hello, I would like some code to get automatically alled in some dynamic libraries at program initialization time (To register some factories from the library in a static class member in a different library). Originally we were using static objects, and registering the factories in their constructors. This works OK on Linux, and seemed to work on Mac, but one library only managed to get a few of these static objects initialized. I browsed some doentation, and it seems that the Mac dynamic linker doesn't initialize all static C++ objects when loading the library. I tried moving the factory registration ...

  1. #1

    Default Dynamic library initialization

    Hello, I would like some code to get automatically alled in some
    dynamic libraries at program initialization time (To register some
    factories from the library in a static class member in a different
    library).

    Originally we were using static objects, and registering the factories
    in their constructors. This works OK on Linux, and seemed to work on
    Mac, but one library only managed to get a few of these static objects
    initialized. I browsed some doentation, and it seems that the Mac
    dynamic linker doesn't initialize all static C++ objects when loading
    the library.

    I tried moving the factory registration code into an "_init" function
    (And used the linker flag -Wl,-init,_init) but that didn't seem to
    work either. The linker docs and some web pages mention that the
    init function is only called when the library is first "used". I
    guess simply referencing the class name when I try to use the factory
    is not enough.

    So it looks like my options are to call the initialization function
    manually from every program (trying to avoid that with this automatic
    registration), play weird tricks with CPP macros, or somehow change
    the behavior of the linker.

    Any advice on the best way to do this?

    thanks

    reed
    Reed Guest

  2. #2

    Default Re: Dynamic library initialization

    In article <google.com>,
    net (Reed Hedges) wrote:
     

    Try linking your library with -single_module (and maybe -bind_at_load).
    That might cause all of your static objects to be created when the first
    one is referenced.

    -Eric

    --
    Eric Albert stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  3. #3

    Default Re: Dynamic library initialization

    Eric Albert <stanford.edu> wrote in message news:<stanford.edu>... 

    Ah, thanks. I saw those options in the manual but they didn't seem to
    do exactly what I need. (Maybe I will experiment with them though)

    A key port of the problem is that the static objects aren't being
    referenced before the point in my program where I need them to have
    been initialized.

    I ended up solving the problem by making a local reference to the
    static object in my program. (The library uses a macro in a public
    header file to do it)

    It also worked when I did the initialization in a function with
    "__attribute__ ((constructor))" but that is a GCC extension, and not
    as portable.

    I'll post a short explanation of the problem and solutions on my web
    page at some point (http://zerohour.net/~reed) in case anyone else has
    this problem.


    reed
    Reed Guest

Similar Threads

  1. how to include dynamic library?
    By neele in forum Mac Programming
    Replies: 25
    Last Post: February 11th, 06:44 AM
  2. HELP Please, Unable to load dynamic library
    By Rob in forum PHP Development
    Replies: 4
    Last Post: February 8th, 09:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139