unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* modules and C
@ 2007-03-21 22:57 David Fang
  0 siblings, 0 replies; 4+ messages in thread
From: David Fang @ 2007-03-21 22:57 UTC (permalink / raw)
  To: guile-user

Hi,
	I'm having a bit of trouble using a symbol I defined in C from a
module:

	In a .cc file, I've defined some_C_function(), and exposed it to
guile via scm_c_define_gsubr("some-C-function", ...), which is called upon
initialization as part of an inner_main() passed to scm_shell().  The
program loads up fine, and I'm able to interactively call
(some-C-function) in the interpreter.
	In a .scm module file, I (define-module (foo bar)) and
(define-public (blah x) (some-C-function x)).
I run my program again (with a GUILE_LOAD_PATH to the new .scm),
guile> (use-modules (foo bar))
I see both 'some-C-function' and 'blah' are available procedures, but as
soon as I call (blah ...), I get "Unbound variable: some-C-function"

guile> blah
#<procedure blah (x)>
guile> some-C-function
#<primitive-procedure some-C-function>

guile> (blah 1)

Backtrace:
In current input:
   5: 0* [blah <raw-chpsim-trace-stream>]
In ../../../../src/scm/foo/bar.scm:
  48: 1  (some-C-function x)

which indicates that my C-wrapped functions aren't visible to the newly
defined module, even through they are present in the interpreter's
environment.  What do I need to do to export my scm_c_define_gsubr'd
functions to the module?  Must I wrap them into another module in C, and
use-module it?  (Would I expect the same problem with mixing
load-extensions with modules?)

I've been reading the manuals and info on the module's sections and didn't
find a satisfactory answer.  (Is this a 'quirk?')  What basic concept(s)
am I missing here?  Thanks in advance!


Fang



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: modules and C
@ 2007-03-22  0:17 dsmich
  2007-03-23  0:19 ` modules and C,C++ David Fang
  0 siblings, 1 reply; 4+ messages in thread
From: dsmich @ 2007-03-22  0:17 UTC (permalink / raw)
  To: David Fang; +Cc: guile-user


---- David Fang <fang@csl.cornell.edu> wrote: 
> Hi,
> 	I'm having a bit of trouble using a symbol I defined in C from a
> module:

 ....
> What do I need to do to export my scm_c_define_gsubr'd
> functions to the module?  Must I wrap them into another module in C, and
> use-module it?  (Would I expect the same problem with mixing
> load-extensions with modules?)
> 
> I've been reading the manuals and info on the module's sections and didn't
> find a satisfactory answer.  (Is this a 'quirk?')  What basic concept(s)
> am I missing here?  Thanks in advance!

You will want to read about scm_c_export() and scm_c_call_with_current_module in http://www.gnu.org/software/guile/manual/html_node/Dynamic-Linking-and-Compiled-Code-Modules.html
and http://www.gnu.org/software/guile/manual/html_node/Accessing-Modules-from-C.html

-Dale



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: modules and C,C++
  2007-03-22  0:17 modules and C dsmich
@ 2007-03-23  0:19 ` David Fang
  0 siblings, 0 replies; 4+ messages in thread
From: David Fang @ 2007-03-23  0:19 UTC (permalink / raw)
  To: dsmich; +Cc: guile-user

>  ....
> > What do I need to do to export my scm_c_define_gsubr'd
> > functions to the module?  Must I wrap them into another module in C, and
> > use-module it?  (Would I expect the same problem with mixing
> > load-extensions with modules?)
>
> You will want to read about scm_c_export() and scm_c_call_with_current_module in http://www.gnu.org/software/guile/manual/html_node/Dynamic-Linking-and-Compiled-Code-Modules.html
> and http://www.gnu.org/software/guile/manual/html_node/Accessing-Modules-from-C.html

Thanks for the pointer.  What I ended up doing was wrapping the
scm_c_define_gsubr initialization routine (along with scm_c_export's)
inside a scm_c_define_module("foo bar-primitives", ...) to distinguish the
primitives from the non-primitives in a pure scheme module that calls the
primitives.  This way I don't end up throwing things into the default
(guile-user) top module.  Works quite well.

Fang



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: modules and C
@ 2007-03-23  7:01 Marco Maggi
  0 siblings, 0 replies; 4+ messages in thread
From: Marco Maggi @ 2007-03-23  7:01 UTC (permalink / raw)
  To: guile-user

"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 */

   void
   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
   module:

   #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:

   void
   my_module_init (void)
   {
     /* other init stuff */

   #ifndef SCM_MAGIC_SNARFER
   #  include "module.x"
   #endif
   }

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




_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-03-23  7:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-03-22  0:17 modules and C dsmich
2007-03-23  0:19 ` modules and C,C++ David Fang
  -- strict thread matches above, loose matches on Subject: below --
2007-03-23  7:01 modules and C Marco Maggi
2007-03-21 22:57 David Fang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).