gcc option for constructor and destructors - Unix

This is a discussion on gcc option for constructor and destructors - Unix ; i want to write a constructor and destructor function in c for each of the function. I should register a routines which will be called before calling the function and destructor before actual return of the original function. Does gcc ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: gcc option for constructor and destructors

  1. gcc option for constructor and destructors

    i want to write a constructor and destructor function in c for each of
    the function.
    I should register a routines which will be called before calling the
    function and destructor before actual return of the original function.
    Does gcc provides anything like this.

    thanks
    sinbad

  2. Re: gcc option for constructor and destructors

    On Apr 8, 11:26*pm, sinbad wrote:
    > i want to write a constructor and destructor function in c for each of
    > the function.
    > I should register a routines which will be called before calling the
    > function and destructor before actual return of the original function.
    > Does gcc provides anything like this.
    >
    > thanks
    > sinbad


    C does not have constructors or destructors.
    --
    Fred Kleinschmidt

  3. Re: gcc option for constructor and destructors

    On Apr 9, 7:26 am, sinbad wrote:
    > i want to write a constructor and destructor function in c for each of
    > the function.
    > I should register a routines which will be called before calling the
    > function and destructor before actual return of the original function.
    > Does gcc provides anything like this.


    gcc allows you to put a cleanup attribute on one of the
    automatic variables which would give you the destructor.
    http://gcc.gnu.org/onlinedocs/gcc-4....e%20Attributes

    To simulate a constructor, you can overload the function
    by giving your constructor the same name as the
    function and then use dlsym( RTLD_NEXT,...) to
    call the function itself.

    You're probably better off just modifying
    the function to start by calling your constructor/initializer
    and end by calling the destructor.

  4. Re: gcc option for constructor and destructors

    sinbad writes:

    > i want to write a constructor and destructor function in c for each of
    > the function.
    > I should register a routines which will be called before calling the
    > function and destructor before actual return of the original function.
    > Does gcc provides anything like this.


    I don't completely understand the question, but maybe this does what you
    want?

    #include

    static __attribute__((constructor)) void ctor(void)
    {
    puts("Hello, world from ctor");
    }

    int main(int argc, char *argv[])
    {
    puts("Hello, world from main");
    return 0;
    }

    $ gcc foo.c && ./a.out
    Hello, world from ctor
    Hello, world from main

    Chip

    --
    Charles M. "Chip" Coldwell
    "Turn on, log in, tune out"
    GPG Key ID: 852E052F
    GPG Key Fingerprint: 77E5 2B51 4907 F08A 7E92 DE80 AFA9 9A8F 852E 052F

+ Reply to Thread