* eval.c (scm_promise_p), list.c (scm_append_x, scm_reverse_x),
[bpt/guile.git] / doc / scheme-modules.texi
1 @page
2 @node Modules
3 @chapter Modules
4 @cindex modules
5
6 [FIXME: somewhat babbling; should be reviewed by someone who understands
7 modules, once the new module system is in place]
8
9 When programs become large, naming conflicts can occur when a function
10 or global variable defined in one file has the same name as a function
11 or global variable in another file. Even just a @emph{similarity}
12 between function names can cause hard-to-find bugs, since a programmer
13 might type the wrong function name.
14
15 The approach used to tackle this problem is called @emph{information
16 encapsulation}, which consists of packaging functional units into a
17 given name space that is clearly separated from other name spaces.
18 @cindex encapsulation
19 @cindex information encapsulation
20 @cindex name space
21
22 The language features that allow this are usually called @emph{the
23 module system} because programs are broken up into modules that are
24 compiled separately (or loaded separately in an interpreter).
25
26 Older languages, like C, have limited support for name space
27 manipulation and protection. In C a variable or function is public by
28 default, and can be made local to a module with the @code{static}
29 keyword. But you cannot reference public variables and functions from
30 another module with different names.
31
32 More advanced module systems have become a common feature in recently
33 designed languages: ML, Python, Perl, and Modula 3 all allow the
34 @emph{renaming} of objects from a foreign module, so they will not
35 clutter the global name space.
36 @cindex name space - private
37
38 @menu
39 * Scheme and modules:: How modules are handled in standard Scheme.
40 * The Guile module system:: How Guile does it.
41 * Dynamic Libraries:: Loading libraries of compiled code at run time.
42 @end menu
43
44
45 @node Scheme and modules
46 @section Scheme and modules
47
48 Scheme, as defined in R5RS, does @emph{not} have a module system at all.
49
50 Aubrey Jaffer, mostly to support his portable Scheme library SLIB,
51 implemented a provide/require mechanism for many Scheme implementations.
52 Library files in SLIB @emph{provide} a feature, and when user programs
53 @emph{require} that feature, the library file is loaded in.
54
55 For example, the file @file{random.scm} in the SLIB package contains the
56 line
57
58 @smalllisp
59 (provide 'random)
60 @end smalllisp
61
62 so to use its procedures, a user would type
63
64 @smalllisp
65 (require 'random)
66 @end smalllisp
67
68 and they would magically become available, @emph{but still have the same
69 names!} So this method is nice, but not as good as a full-featured
70 module system.
71
72
73 @node The Guile module system
74 @section The Guile module system
75
76 In 1996 Tom Lord implemented a full-featured module system for Guile
77 which allows loading Scheme source files into a private name space.
78
79 This module system is regarded as being rather idiosyncratic, and will
80 probably change to something more like the ML module system, so for now
81 I will simply describe how it works for a couple of simple cases.
82
83 So for example, the pipe interprocess communication interface
84 (REFFIXME), contained in @file{$srcdir/ice-9/popen.scm}, starts out with
85
86 @smalllisp
87 (define-module (ice-9 popen))
88 @end smalllisp
89
90 and a user program can use
91
92 @smalllisp
93 (use-modules (ice-9 popen))
94 @end smalllisp
95
96 to have access to all procedures and variables exported from the module.
97
98 @menu
99 * General Information about Modules:: Guile module basics.
100 * Loading Guile Modules:: How to use existing modules.
101 * Creating Guile Modules:: How to package your code into modules.
102 * More Module Procedures:: Low-level module code.
103 * Included Guile Modules:: Which modules come with Guile?
104 @end menu
105
106 @node General Information about Modules
107 @subsection General Information about Modules
108
109 A Guile module is a collection of procedures, variables and syntactic
110 forms (macros), which are either public or private. Public bindings are
111 in the so-called @dfn{export list} of a module and can be made visible
112 to other modules, which import them. This @dfn{module import} is called
113 @dfn{using} of a module, and consists of loading of the module code (if
114 it has not already been loaded) and making all exported items of the
115 loaded module visible to the importing module (@pxref{Loading Guile
116 Modules}).
117
118 The other side is called @dfn{defining} a module, and consists of giving
119 a name to a module, add procedures and variables to it and declare which
120 of the names should be exported when another module uses it
121 (@pxref{Creating Guile Modules}).
122
123 All Guile modules have unique names, which are lists of one or more
124 symbols. Examples are @code{(ice-9 popen)} or @code{(srfi srfi-11)}.
125 When Guile searches for the code of a module, it constructs the name of
126 the file to load by concatenating the name elements with slashes between
127 the elements and appending a number of file name extensions from the
128 list @code{%load-extensions} (REFFIXME). The resulting file name is
129 then searched in all directories in the variable @code{%load-path}. For
130 example, the @code{(ice-9 popen)} module would result in the filename
131 @code{ice-9/popen.scm} and searched in the installation directory of
132 Guile and in all other directories in the load path.
133
134 @c FIXME::martin: Not sure about this, maybe someone knows better?
135 Every module has a so-called syntax transformer associated with it.
136 This is a procedure which performs all syntax transformation for the
137 time the module is read in and evaluated. When working with modules,
138 you can manipulate the current syntax transformer using the
139 @code{use-syntax} syntactic form or the @code{#:use-syntax} module
140 definition option (@pxref{Creating Guile Modules}).
141
142 Please note that there are some problems with the current module system
143 you should keep in mind. When importing a module which exports a macro
144 definition, the other module must export all bindings the macro
145 expansion uses, too, because the expanded code would otherwise not be
146 able to see these definitions and issue a ``variable unbound'' error, or
147 worse, would use another binding which might be present in the scope of
148 the expansion.
149
150 When two or more modules are imported, and they export bindings with the
151 same names, the last imported module wins, and the exported binding of
152 that last module will silently be used. This might lead to
153 hard-to-find errors because wrong procedures or variables are used.
154
155
156 @node Loading Guile Modules
157 @subsection Loading Guile Modules
158
159 @c FIXME::martin: Review me!
160
161 There are several modules included in the Guile distribution, and not
162 all of the procedures available for Guile are immedietely available when
163 you start up the interpreter. Some of the procedures are packaged in
164 modules, so that they are only accessible after the user has explicitly
165 said that she wants to use them. In Guile, the syntactic form
166 @code{use-modules} is used for telling the interpreter that he should
167 locate the code for a given module, load it and make the exported
168 bindings of the module visible to the caller.
169
170 @c begin (scm-doc-string "boot-9.scm" "use-modules")
171 @deffn syntax use-modules module-specification @dots{}
172 All @var{module-specification}s are of the form @code{(hierarchy file)}.
173 One example of this is
174
175 @smalllisp
176 (use-modules (ice-9 popen))
177 @end smalllisp
178
179 @code{use-modules} allows the current Guile program to use all publicly
180 defined procedures and variables in the modules denoted by the
181 @var{module-specification}s.
182 @end deffn
183 @c end
184
185 @c FIXME::martin: Is this correct, and is there more to say?
186 @c FIXME::martin: Define term and concept `system transformer' somewhere.
187
188 @deffn syntax use-syntax module-specification
189 Load the module @code{module-specification} and use its system
190 transformer as the system transformer for the currently defined module,
191 as well as installing it as the current system transformer.
192 @end deffn
193
194
195 @node Creating Guile Modules
196 @subsection Creating Guile Modules
197
198 @c FIXME::martin: Review me!
199
200 When you want to create your own modules, you have to take the following
201 steps:
202
203 @itemize @bullet
204 @item
205 Create a Scheme source file and add all variables and procedures you wish
206 to export, or which are required by the exported procedures.
207
208 @item
209 Add a @code{define-module} form at the beginning.
210
211 @item
212 Export all bindings which should be visible to importing modules, either
213 by using @code{define-public} or @code{export} (both documented below).
214 @end itemize
215
216 @c begin (scm-doc-string "boot-9.scm" "define-module")
217 @deffn syntax define-module module-specification [options @dots{}]
218 @var{module-specification} is of the form @code{(hierarchy file)}. One
219 example of this is
220
221 @smalllisp
222 (define-module (ice-9 popen))
223 @end smalllisp
224
225 @code{define-module} makes this module available to Guile programs under
226 the given @var{module-specification}.
227
228 The @var{options} are keyword/value pairs which specify more about the
229 defined module. The recognized options and their meaning is shown in
230 the following table.
231
232 @table @code
233 @item #:use-module @var{module}
234 Equivalent to a @code{(use-modules @var{module})}. Use the specified
235 @var{module} when loading this module.
236
237 @item #:use-syntax @var{module}
238 Use @var{module} when loading the currently defined module, and install
239 it as the syntax transformer.
240
241 @item #:autoload @var{module} @var{symbol}
242 Load @var{module} whenever @var{symbol} is accessed.
243
244 @item #:export @var{list}
245 Export all identifiers in @var{list}, which must be a list of symbols.
246 This is equivalent to @code{(export @var{list})} in the module body.
247
248 @item #:no-backtrace
249 Tell Guile not to record information for procedure backtraces when
250 executing the procedures in this module.
251
252 @item #:pure
253 Create a @dfn{pure} module, that is a module which does not contain any
254 of the standard procedure bindings except for the syntax forms. This is
255 useful if you want to create @dfn{safe} modules, that is modules which
256 do not know anything about dangerous procedures.
257 @end table
258
259 @end deffn
260 @c end
261
262 @deffn syntax export variable @dots{}
263 Add all @var{variable}s (which must be symbols) to the list of exported
264 bindings of the current module.
265 @end deffn
266
267 @c begin (scm-doc-string "boot-9.scm" "define-public")
268 @deffn syntax define-public @dots{}
269 Makes a procedure or variable available to programs that use the current
270 module.
271 @end deffn
272 @c end
273
274
275 [FIXME: must say more, and explain, and also demonstrate a private name
276 space use, and demonstrate how one would do Python's "from Tkinter
277 import *" versus "import Tkinter". Must also add something about paths
278 and standards for contributed modules.]
279
280 @node More Module Procedures
281 @subsection More Module Procedures
282
283 @c FIXME::martin: Review me!
284
285 @c FIXME::martin: Should this procedure be documented and supported
286 @c at all?
287
288 The procedures in this section are useful if you want to dig into the
289 innards of Guile's module system. If you don't know precisely what you
290 do, you should probably avoid using any of them.
291
292 @deffn primitive standard-eval-closure module
293 Return an eval closure for the module @var{module}.
294 @end deffn
295
296
297 @node Included Guile Modules
298 @subsection Included Guile Modules
299
300 @c FIXME::martin: Review me!
301
302 Some modules are included in the Guile distribution; here are references
303 to the entries in this manual which describe them in more detail:
304
305 @table @strong
306 @item boot-9
307 boot-9 is Guile's initialization module, and it is always loaded when
308 Guile starts up.
309
310 @item (ice-9 debug)
311 Mikael Djurfeldt's source-level debugging support for Guile
312 (@pxref{Debugger User Interface}).
313
314 @item (ice-9 threads)
315 Guile's support for multi threaded execution (@pxref{Scheduling}).
316
317 @item (ice-9 rdelim)
318 Line- and character-delimited input (REFFIXME).
319
320 @item (ice-9 documentation)
321 Online documentation (REFFIXME).
322
323 @item (srfi srfi-2)
324 Support for @code{and-let*} (REFFIXME).
325
326 @item (srfi srfi-6)
327 Support for some additional string port procedures (REFFIXME).
328
329 @item (srfi srfi-8)
330 Multiple-value handling with @code{receive} (REFFIXME).
331
332 @item (srfi srfi-9)
333 Record definition with @code{define-record-type} (REFFIXME).
334
335 @item (srfi srfi-10)
336 Read hash extension @code{#,()} (REFFIXME).
337
338 @item (srfi srfi-11)
339 Multiple-value handling with @code{let-values} and @code{let-values*}
340 (REFFIXME).
341
342 @item (srfi srfi-13)
343 String library (REFFIXME).
344
345 @item (srfi srfi-14)
346 Character-set library (REFFIXME).
347
348 @item (srfi srfi-17)
349 Getter-with-setter support (REFFIXME).
350
351 @item (ice-9 slib)
352 This module contains hooks for using Aubrey Jaffer's portable Scheme
353 library SLIB from Guile (@pxref{SLIB}).
354
355 @c FIXME::martin: This module is not in the distribution. Remove it
356 @c from here?
357 @item (ice-9 jacal)
358 This module contains hooks for using Aubrey Jaffer's symbolic math
359 packge Jacal from Guile (@pxref{JACAL}).
360 @end table
361
362
363 @node Dynamic Libraries
364 @section Dynamic Libraries
365
366 Most modern Unices have something called @dfn{shared libraries}. This
367 ordinarily means that they have the capability to share the executable
368 image of a library between several running programs to save memory and
369 disk space. But generally, shared libraries give a lot of additional
370 flexibility compared to the traditional static libraries. In fact,
371 calling them `dynamic' libraries is as correct as calling them `shared'.
372
373 Shared libraries really give you a lot of flexibility in addition to the
374 memory and disk space savings. When you link a program against a shared
375 library, that library is not closely incorporated into the final
376 executable. Instead, the executable of your program only contains
377 enough information to find the needed shared libraries when the program
378 is actually run. Only then, when the program is starting, is the final
379 step of the linking process performed. This means that you need not
380 recompile all programs when you install a new, only slightly modified
381 version of a shared library. The programs will pick up the changes
382 automatically the next time they are run.
383
384 Now, when all the necessary machinery is there to perform part of the
385 linking at run-time, why not take the next step and allow the programmer
386 to explicitly take advantage of it from within his program? Of course,
387 many operating systems that support shared libraries do just that, and
388 chances are that Guile will allow you to access this feature from within
389 your Scheme programs. As you might have guessed already, this feature
390 is called @dfn{dynamic linking}@footnote{Some people also refer to the
391 final linking stage at program startup as `dynamic linking', so if you
392 want to make yourself perfectly clear, it is probably best to use the
393 more technical term @dfn{dlopening}, as suggested by Gordon Matzigkeit
394 in his libtool documentation.}
395
396 As with many aspects of Guile, there is a low-level way to access the
397 dynamic linking apparatus, and a more high-level interface that
398 integrates dynamically linked libraries into the module system.
399
400 @menu
401 * Low level dynamic linking::
402 * Compiled Code Modules::
403 * Dynamic Linking and Compiled Code Modules::
404 @end menu
405
406 @node Low level dynamic linking
407 @subsection Low level dynamic linking
408
409 When using the low level procedures to do your dynamic linking, you have
410 complete control over which library is loaded when and what get's done
411 with it.
412
413 @deffn primitive dynamic-link library
414 Find the shared library denoted by @var{library} (a string) and link it
415 into the running Guile application. When everything works out, return a
416 Scheme object suitable for representing the linked object file.
417 Otherwise an error is thrown. How object files are searched is system
418 dependent.
419
420 Normally, @var{library} is just the name of some shared library file
421 that will be searched for in the places where shared libraries usually
422 reside, such as in @file{/usr/lib} and @file{/usr/local/lib}.
423 @end deffn
424
425 @deffn primitive dynamic-object? val
426 Determine whether @var{val} represents a dynamically linked object file.
427 @end deffn
428
429 @deffn primitive dynamic-unlink dynobj
430 Unlink the indicated object file from the application. The argument
431 @var{dynobj} should be one of the values returned by
432 @code{dynamic-link}. When @code{dynamic-unlink} has been called on
433 @var{dynobj}, it is no longer usable as an argument to the functions
434 below and you will get type mismatch errors when you try to.
435 @end deffn
436
437 @deffn primitive dynamic-func function dynobj
438 Search the C function indicated by @var{function} (a string or symbol)
439 in @var{dynobj} and return some Scheme object that can later be used
440 with @code{dynamic-call} to actually call this function. Right now,
441 these Scheme objects are formed by casting the address of the function
442 to @code{long} and converting this number to its Scheme representation.
443
444 Regardless whether your C compiler prepends an underscore @samp{_} to
445 the global names in a program, you should @strong{not} include this
446 underscore in @var{function}. Guile knows whether the underscore is
447 needed or not and will add it when necessary.
448 @end deffn
449
450 @deffn primitive dynamic-call function dynobj
451 Call the C function indicated by @var{function} and @var{dynobj}. The
452 function is passed no arguments and its return value is ignored. When
453 @var{function} is something returned by @code{dynamic-func}, call that
454 function and ignore @var{dynobj}. When @var{function} is a string (or
455 symbol, etc.), look it up in @var{dynobj}; this is equivalent to
456
457 @smallexample
458 (dynamic-call (dynamic-func @var{function} @var{dynobj} #f))
459 @end smallexample
460
461 Interrupts are deferred while the C function is executing (with
462 @code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).
463 @end deffn
464
465 @deffn primitive dynamic-args-call function dynobj args
466 Call the C function indicated by @var{function} and @var{dynobj}, just
467 like @code{dynamic-call}, but pass it some arguments and return its
468 return value. The C function is expected to take two arguments and
469 return an @code{int}, just like @code{main}:
470
471 @smallexample
472 int c_func (int argc, char **argv);
473 @end smallexample
474
475 The parameter @var{args} must be a list of strings and is converted into
476 an array of @code{char *}. The array is passed in @var{argv} and its
477 size in @var{argc}. The return value is converted to a Scheme number
478 and returned from the call to @code{dynamic-args-call}.
479 @end deffn
480
481 When dynamic linking is disabled or not supported on your system,
482 the above functions throw errors, but they are still available.
483
484 Here is a small example that works on GNU/Linux:
485
486 @smallexample
487 (define libc-obj (dynamic-link "libc.so"))
488 libc-obj
489 @result{} #<dynamic-object "libc.so">
490 (dynamic-args-call 'rand libc-obj '())
491 @result{} 269167349
492 (dynamic-unlink libc-obj)
493 libc-obj
494 @result{} #<dynamic-object "libc.so" (unlinked)>
495 @end smallexample
496
497 As you can see, after calling @code{dynamic-unlink} on a dynamically
498 linked library, it is marked as @samp{(unlinked)} and you are no longer
499 able to use it with @code{dynamic-call}, etc. Whether the library is
500 really removed from you program is system-dependent and will generally
501 not happen when some other parts of your program still use it. In the
502 example above, @code{libc} is almost certainly not removed from your
503 program because it is badly needed by almost everything.
504
505 The functions to call a function from a dynamically linked library,
506 @code{dynamic-call} and @code{dynamic-args-call}, are not very powerful.
507 They are mostly intended to be used for calling specially written
508 initialization functions that will then add new primitives to Guile.
509 For example, we do not expect that you will dynamically link
510 @file{libX11} with @code{dynamic-link} and then construct a beautiful
511 graphical user interface just by using @code{dynamic-call} and
512 @code{dynamic-args-call}. Instead, the usual way would be to write a
513 special Guile<->X11 glue library that has intimate knowledge about both
514 Guile and X11 and does whatever is necessary to make them inter-operate
515 smoothly. This glue library could then be dynamically linked into a
516 vanilla Guile interpreter and activated by calling its initialization
517 function. That function would add all the new types and primitives to
518 the Guile interpreter that it has to offer.
519
520 From this setup the next logical step is to integrate these glue
521 libraries into the module system of Guile so that you can load new
522 primitives into a running system just as you can load new Scheme code.
523
524 There is, however, another possibility to get a more thorough access to
525 the functions contained in a dynamically linked library. Anthony Green
526 has written @file{libffi}, a library that implements a @dfn{foreign
527 function interface} for a number of different platforms. With it, you
528 can extend the Spartan functionality of @code{dynamic-call} and
529 @code{dynamic-args-call} considerably. There is glue code available in
530 the Guile contrib archive to make @file{libffi} accessible from Guile.
531
532 @node Compiled Code Modules
533 @subsection Putting Compiled Code into Modules
534
535 The new primitives that you add to Guile with @code{gh_new_procedure} or
536 with any of the other mechanisms are normally placed into the same
537 module as all the other builtin procedures (like @code{display}).
538 However, it is also possible to put new primitives into their own
539 module.
540
541 The mechanism for doing so is not very well thought out and is likely to
542 change when the module system of Guile itself is revised, but it is
543 simple and useful enough to document it as it stands.
544
545 What @code{gh_new_procedure} and the functions used by the snarfer
546 really do is to add the new primitives to whatever module is the
547 @emph{current module} when they are called. This is analogous to the
548 way Scheme code is put into modules: the @code{define-module} expression
549 at the top of a Scheme source file creates a new module and makes it the
550 current module while the rest of the file is evaluated. The
551 @code{define} expressions in that file then add their new definitions to
552 this current module.
553
554 Therefore, all we need to do is to make sure that the right module is
555 current when calling @code{gh_new_procedure} for our new primitives.
556 Unfortunately, there is not yet an easy way to access the module system
557 from C, so we are better off with a more indirect approach. Instead of
558 adding our primitives at initialization time we merely register with
559 Guile that we are ready to provide the contents of a certain module,
560 should it ever be needed.
561
562 @deftypefun void scm_register_module_xxx (char *@var{name}, void (*@var{initfunc})(void))
563 Register with Guile that @var{initfunc} will provide the contents of the
564 module @var{name}.
565
566 The function @var{initfunc} should perform the usual initialization
567 actions for your new primitives, like calling @code{gh_new_procedure} or
568 including the file produced by the snarfer. When @var{initfunc} is
569 called, the current module is a newly created module with a name as
570 indicated by @var{name}. Each definition that is added to it will be
571 automatically exported.
572
573 The string @var{name} indicates the hierachical name of the new module.
574 It should consist of the individual components of the module name
575 separated by single spaces. That is, the Scheme module name @code{(foo
576 bar)}, which is a list, should be written as @code{"foo bar"} for the
577 @var{name} parameter.
578
579 You can call @code{scm_register_module_xxx} at any time, even before
580 Guile has been initialized. This might be useful when you want to put
581 the call to it in some initialization code that is magically called
582 before main, like constructors for global C++ objects.
583
584 An example for @code{scm_register_module_xxx} appears in the next section.
585 @end deftypefun
586
587 Now, instead of calling the initialization function at program startup,
588 you should simply call @code{scm_register_module_xxx} and pass it the
589 initialization function. When the named module is later requested by
590 Scheme code with @code{use-modules} for example, Guile will notice that
591 it knows how to create this module and will call the initialization
592 function at the right time in the right context.
593
594 @node Dynamic Linking and Compiled Code Modules
595 @subsection Dynamic Linking and Compiled Code Modules
596
597 The most interesting application of dynamically linked libraries is
598 probably to use them for providing @emph{compiled code modules} to
599 Scheme programs. As much fun as programming in Scheme is, every now and
600 then comes the need to write some low-level C stuff to make Scheme even
601 more fun.
602
603 Not only can you put these new primitives into their own module (see the
604 previous section), you can even put them into a shared library that is
605 only then linked to your running Guile image when it is actually
606 needed.
607
608 An example will hopefully make everything clear. Suppose we want to
609 make the Bessel functions of the C library available to Scheme in the
610 module @samp{(math bessel)}. First we need to write the appropriate
611 glue code to convert the arguments and return values of the functions
612 from Scheme to C and back. Additionally, we need a function that will
613 add them to the set of Guile primitives. Because this is just an
614 example, we will only implement this for the @code{j0} function, tho.
615
616 @smallexample
617 #include <math.h>
618 #include <guile/gh.h>
619
620 SCM
621 j0_wrapper (SCM x)
622 @{
623 return gh_double2scm (j0 (gh_scm2double (x)));
624 @}
625
626 void
627 init_math_bessel ()
628 @{
629 gh_new_procedure1_0 ("j0", j0_wrapper);
630 @}
631 @end smallexample
632
633 We can already try to bring this into action by manually calling the low
634 level functions for performing dynamic linking. The C source file needs
635 to be compiled into a shared library. Here is how to do it on
636 GNU/Linux, please refer to the @code{libtool} documentation for how to
637 create dynamically linkable libraries portably.
638
639 @smallexample
640 gcc -shared -o libbessel.so -fPIC bessel.c
641 @end smallexample
642
643 Now fire up Guile:
644
645 @smalllisp
646 (define bessel-lib (dynamic-link "./libbessel.so"))
647 (dynamic-call "init_math_bessel" bessel-lib)
648 (j0 2)
649 @result{} 0.223890779141236
650 @end smalllisp
651
652 The filename @file{./libbessel.so} should be pointing to the shared
653 library produced with the @code{gcc} command above, of course. The
654 second line of the Guile interaction will call the
655 @code{init_math_bessel} function which in turn will register the C
656 function @code{j0_wrapper} with the Guile interpreter under the name
657 @code{j0}. This function becomes immediately available and we can call
658 it from Scheme.
659
660 Fun, isn't it? But we are only half way there. This is what
661 @code{apropos} has to say about @code{j0}:
662
663 @smallexample
664 (apropos 'j0)
665 @print{} the-root-module: j0 #<primitive-procedure j0>
666 @end smallexample
667
668 As you can see, @code{j0} is contained in the root module, where all
669 the other Guile primitives like @code{display}, etc live. In general,
670 a primitive is put into whatever module is the @dfn{current module} at
671 the time @code{gh_new_procedure} is called. To put @code{j0} into its
672 own module named @samp{(math bessel)}, we need to make a call to
673 @code{scm_register_module_xxx}. Additionally, to have Guile perform
674 the dynamic linking automatically, we need to put @file{libbessel.so}
675 into a place where Guile can find it. The call to
676 @code{scm_register_module_xxx} should be contained in a specially
677 named @dfn{module init function}. Guile knows about this special name
678 and will call that function automatically after having linked in the
679 shared library. For our example, we add the following code to
680 @file{bessel.c}:
681
682 @smallexample
683 void scm_init_math_bessel_module ()
684 @{
685 scm_register_module_xxx ("math bessel", init_math_bessel);
686 @}
687 @end smallexample
688
689 The general pattern for the name of a module init function is:
690 @samp{scm_init_}, followed by the name of the module where the
691 individual hierarchical components are concatenated with underscores,
692 followed by @samp{_module}. It should call
693 @code{scm_register_module_xxx} with the correct module name and the
694 appropriate initialization function. When that initialization function
695 will be called, a newly created module with the right name will be the
696 @emph{current module} so that all definitions that the initialization
697 functions makes will end up in the correct module.
698
699 After @file{libbessel.so} has been rebuild, we need to place the shared
700 library into the right place. When Guile tries to autoload the
701 @samp{(math bessel)} module, it looks not only for a file called
702 @file{math/bessel.scm} in its @code{%load-path}, but also for
703 @file{math/libbessel.so}. So all we need to do is to create a directory
704 called @file{math} somewhere in Guile's @code{%load-path} and place
705 @file{libbessel.so} there. Normally, the current directory @file{.} is
706 in the @code{%load-path}, so we just use that for this example.
707
708 @smallexample
709 % mkdir maths
710 % cd maths
711 % ln -s ../libbessel.so .
712 % cd ..
713 % guile
714 guile> (use-modules (math bessel))
715 guile> (j0 2)
716 0.223890779141236
717 guile> (apropos 'j0)
718 @print{} bessel: j0 #<primitive-procedure j0>
719 @end smallexample
720
721 That's it!
722
723 Note that we used a symlink to make @file{libbessel.so} appear in the
724 right spot. This is probably not a bad idea in general. The
725 directories that the @file{%load-path} normally contains are supposed to
726 contain only architecture independent files. They are not really the
727 right place for a shared library. You might want to install the
728 libraries somewhere below @samp{exec_prefix} and then symlink to them
729 from the architecture independent directory. This will at least work on
730 heterogenous systems where the architecture dependent stuff resides in
731 the same place on all machines (which seems like a good idea to me
732 anyway).
733
734
735 @c Local Variables:
736 @c TeX-master: "guile.texi"
737 @c End: