This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: [Q] automagic define/undefine with dynamic-link/dynamic-unlink


> > Any way, now that I see that it *does* work, I have to say that I
> > don't like it.  "_init" and "_fini" seem to be very magical names, but
> > only with ELF.
> 
> I like it a lot.  Using _init means that you can avoid any sort of
> connection between the name of the file to be linked and the name of
> the entry point to initialize the contents of it.

Yes, agreed, having some initialization and finalization code run
automatically is a good thing.

But the _init(), _fini() functions--at least as they are done on
Linux--are not my favorite interface to accessing that feature.  They
are strictly an internal feature of the C runtime (as suggested by
their name), and you can't use them in any flexible way.

You can't link code that uses them into a program, _init and _fini are
already used by the startup code.  You will probably also collide with
the C++ compiler.

I think the proper way to use that feature is

    void my_init () __attribute__ ((constructor));

    void my_init ()
    {
      ...
    }

likewise for the "destructor" attribute.  Works only with GCC, of
course, but is independent from the ELF format, or a particular
implementation of it.

It also has the advantage of pouring some LISP into your C. ;-)