Merge commit '58147d67806e1f54c447d7eabac35b1a5086c3a6'
[bpt/guile.git] / doc / ref / libguile-extensions.texi
CommitLineData
3229f68b
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
7e23d9d0 3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2011
3229f68b
MV
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
7@node Linking Guile with Libraries
8@section Linking Guile with Libraries
9
10The previous section has briefly explained how to write programs that
11make use of an embedded Guile interpreter. But sometimes, all you
12want to do is make new primitive procedures and data types available
13to the Scheme programmer. Writing a new version of @code{guile} is
14inconvenient in this case and it would in fact make the life of the
15users of your new features needlessly hard.
16
17For example, suppose that there is a program @code{guile-db} that is a
18version of Guile with additional features for accessing a database.
19People who want to write Scheme programs that use these features would
20have to use @code{guile-db} instead of the usual @code{guile} program.
21Now suppose that there is also a program @code{guile-gtk} that extends
22Guile with access to the popular Gtk+ toolkit for graphical user
23interfaces. People who want to write GUIs in Scheme would have to use
24@code{guile-gtk}. Now, what happens when you want to write a Scheme
25application that uses a GUI to let the user access a database? You
26would have to write a @emph{third} program that incorporates both the
27database stuff and the GUI stuff. This might not be easy (because
28@code{guile-gtk} might be a quite obscure program, say) and taking this
29example further makes it easy to see that this approach can not work in
30practice.
31
32It would have been much better if both the database features and the GUI
33feature had been provided as libraries that can just be linked with
34@code{guile}. Guile makes it easy to do just this, and we encourage you
35to make your extensions to Guile available as libraries whenever
36possible.
37
38You write the new primitive procedures and data types in the normal
39fashion, and link them into a shared library instead of into a
40stand-alone program. The shared library can then be loaded dynamically
41by Guile.
42
43@menu
44* A Sample Guile Extension::
45@end menu
46
47
48@node A Sample Guile Extension
49@subsection A Sample Guile Extension
50
51This section explains how to make the Bessel functions of the C library
52available to Scheme. First we need to write the appropriate glue code
53to convert the arguments and return values of the functions from Scheme
54to C and back. Additionally, we need a function that will add them to
55the set of Guile primitives. Because this is just an example, we will
56only implement this for the @code{j0} function.
57
58Consider the following file @file{bessel.c}.
59
60@smallexample
61#include <math.h>
62#include <libguile.h>
63
64SCM
65j0_wrapper (SCM x)
66@{
7e23d9d0 67 return scm_from_double (j0 (scm_to_double (x)));
3229f68b
MV
68@}
69
70void
71init_bessel ()
72@{
73 scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
74@}
75@end smallexample
76
77This C source file needs to be compiled into a shared library. Here is
78how to do it on GNU/Linux:
79
80@smallexample
4b93693d
AW
81gcc `pkg-config --cflags guile-@value{EFFECTIVE-VERSION}` \
82 -shared -o libguile-bessel.so -fPIC bessel.c
3229f68b
MV
83@end smallexample
84
5f30c653
KR
85For creating shared libraries portably, we recommend the use of GNU
86Libtool (@pxref{Top, , Introduction, libtool, GNU Libtool}).
3229f68b
MV
87
88A shared library can be loaded into a running Guile process with the
89function @code{load-extension}. In addition to the name of the
8c3fa3e5 90library to load, this function also expects the name of a function from
3229f68b
MV
91that library that will be called to initialize it. For our example,
92we are going to call the function @code{init_bessel} which will make
93@code{j0_wrapper} available to Scheme programs with the name
94@code{j0}. Note that we do not specify a filename extension such as
95@file{.so} when invoking @code{load-extension}. The right extension for
96the host platform will be provided automatically.
97
aba0dff5 98@lisp
3229f68b
MV
99(load-extension "libguile-bessel" "init_bessel")
100(j0 2)
101@result{} 0.223890779141236
aba0dff5 102@end lisp
3229f68b
MV
103
104For this to work, @code{load-extension} must be able to find
105@file{libguile-bessel}, of course. It will look in the places that
106are usual for your operating system, and it will additionally look
107into the directories listed in the @code{LTDL_LIBRARY_PATH}
108environment variable.
109
110To see how these Guile extensions via shared libraries relate to the
111module system, @xref{Putting Extensions into Modules}.
5f30c653
KR
112
113
114@c Local Variables:
115@c TeX-master: "guile.texi"
116@c End: