libguile-extensions.texi 4.3 KB

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