next up previous
Next: dlopen Up: Shared Library Previous: Dynamic Linking and Loading

Write the contructor and destructor of a dynamicl library

void
__attribute ((constructor))
    init_function (void) 
{
    printf("hello world\n");
}

void
__attribute ((destructor))
    fini_function (void) 
{
    printf("bye !!! \n");
}

int foo(int a) 
{
    return a + 1;
}
Compile the above code as a dynamicl library , and every time dynamic linker loads the library, the constructor will be invoked, and when the program exists, the destructor will be invoked.

Never use _init and _fini as the constructor and destructor of the library, since they are used by system itself.



Wei Lu 2007-11-06