[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: modules and C

From: Marco Maggi
Subject: Re: modules and C
Date: Fri, 23 Mar 2007 08:01:48 +0100

"David Fang" wrote:
> What do I need to do to export my scm_c_define_gsubr'd
> functions to the module?

YMMV. This is what I do:

1. write a C module with its own initialisation function,
   let's say that the module's file is "module.c" :

   /* C module */

   #include <libguile.h>

   /* module functions here */

   my_module_init (void)

   /* end of module */

   put the init function in an internal header file:

   void my_module_init (void);

2. wrap all the Scheme interface function declarations
   into SCM_DEFINE: for a function with a prototype:

   extern SCM my_scheme_func (SCM arg1, SCM arg2);

   declared in a header file, I write in the body of the

   #undef SFN
   #define SFN       "my-scheme-func"
   SCM_DEFINE(my_scheme_func, SFN,
              2, 0, 0, (SCM arg1, SCM arg2), "")
      SCM s_result;

      /* do something with 'arg1' and 'arg2' */
      return s_result;

   the symbol SFN is defined to be the name of the
   Scheme procedure: that way the name is available
   in the function's body for calls to 'scm_error()',
   for example:

      /* to throw a 'wrong-type-arg' error */
      if (error_condition)
        scm_error(scm_arg_type_key, SFN,
                  "error message",
                  SCM_BOOL_F, SCM_BOOL_F);

3. in the body of the module initialisation function
   put an include for a ".x" file:

   my_module_init (void)
     /* other init stuff */

   #  include "module.x"

4. before compiling: preprocess the "module.c" file
   with the Guile snarfer program; I use a rule in
   the Makefile, something like this

     .SECONDARY: %.x

     GUILE_SNARF = guile-snarf

     %.x : %.c
       $(GUILE_SNARF) $(@) $(<) $(INCLUDES)

     %.o : %.c $.x

   the ".x" file holds the function invocations
   required to define the functions from "module.c"
   something like:

      scm_c_define_gsubr(s_my_scheme_func, 2, 0, 0,
        (SCM (*)()) my_scheme_func); ;

   when the snarfer processes the file the:

     #include "module.x"

   is excluded, but it is included when the compiler
   does its job;

now you can use 'scm_c_call_with_current_module()'
and invoke the initialisation function.

Marco Maggi

reply via email to

[Prev in Thread] Current Thread [Next in Thread]