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