libguile-linking.texi 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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, 2010, 2011
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Linking Programs With Guile
  7. @section Linking Programs With Guile
  8. This section covers the mechanics of linking your program with Guile
  9. on a typical POSIX system.
  10. The header file @code{<libguile.h>} provides declarations for all of
  11. Guile's functions and constants. You should @code{#include} it at the
  12. head of any C source file that uses identifiers described in this
  13. manual. Once you've compiled your source files, you need to link them
  14. against the Guile object code library, @code{libguile}.
  15. @code{<libguile.h>} is not in the default search path for headers,
  16. because Guile supports parallel installation of multiple versions of
  17. Guile, with each version's headers under their own directories. This is
  18. to allow development against, say, both Guile 2.0 and 2.2.
  19. To compile code that includes @code{<libguile.h>}, or links to
  20. @code{libguile}, you need to select the effective version you are
  21. interested in, and then ask @code{pkg-config} for the compilation flags
  22. or linking instructions. For effective version
  23. @value{EFFECTIVE-VERSION}, for example, you would invoke
  24. @code{pkg-config --cflags --libs guile-@value{EFFECTIVE-VERSION}} to get
  25. the compilation and linking flags necessary to link to version
  26. @value{EFFECTIVE-VERSION} of Guile. You would typically run
  27. @code{pkg-config} during the configuration phase of your program and
  28. use the obtained information in the Makefile.
  29. See the @code{pkg-config} man page, for more information.
  30. @menu
  31. * Guile Initialization Functions:: What to call first.
  32. * A Sample Guile Main Program:: Sources and makefiles.
  33. @end menu
  34. @node Guile Initialization Functions
  35. @subsection Guile Initialization Functions
  36. To initialize Guile, you can use one of several functions. The first,
  37. @code{scm_with_guile}, is the most portable way to initialize Guile. It
  38. will initialize Guile when necessary and then call a function that you
  39. can specify. Multiple threads can call @code{scm_with_guile}
  40. concurrently and it can also be called more than once in a given thread.
  41. The global state of Guile will survive from one call of
  42. @code{scm_with_guile} to the next. Your function is called from within
  43. @code{scm_with_guile} since the garbage collector of Guile needs to know
  44. where the stack of each thread is.
  45. A second function, @code{scm_init_guile}, initializes Guile for the
  46. current thread. When it returns, you can use the Guile API in the
  47. current thread. This function employs some non-portable magic to learn
  48. about stack bounds and might thus not be available on all platforms.
  49. One common way to use Guile is to write a set of C functions which
  50. perform some useful task, make them callable from Scheme, and then link
  51. the program with Guile. This yields a Scheme interpreter just like
  52. @code{guile}, but augmented with extra functions for some specific
  53. application --- a special-purpose scripting language.
  54. In this situation, the application should probably process its
  55. command-line arguments in the same manner as the stock Guile
  56. interpreter. To make that straightforward, Guile provides the
  57. @code{scm_boot_guile} and @code{scm_shell} function.
  58. For more about these functions, see @ref{Initialization}.
  59. @node A Sample Guile Main Program
  60. @subsection A Sample Guile Main Program
  61. Here is @file{simple-guile.c}, source code for a @code{main} and an
  62. @code{inner_main} function that will produce a complete Guile
  63. interpreter.
  64. @example
  65. /* simple-guile.c --- how to start up the Guile
  66. interpreter from C code. */
  67. /* Get declarations for all the scm_ functions. */
  68. #include <libguile.h>
  69. static void
  70. inner_main (void *closure, int argc, char **argv)
  71. @{
  72. /* module initializations would go here */
  73. scm_shell (argc, argv);
  74. @}
  75. int
  76. main (int argc, char **argv)
  77. @{
  78. scm_boot_guile (argc, argv, inner_main, 0);
  79. return 0; /* never reached */
  80. @}
  81. @end example
  82. The @code{main} function calls @code{scm_boot_guile} to initialize
  83. Guile, passing it @code{inner_main}. Once @code{scm_boot_guile} is
  84. ready, it invokes @code{inner_main}, which calls @code{scm_shell} to
  85. process the command-line arguments in the usual way.
  86. Here is a Makefile which you can use to compile the above program. It
  87. uses @code{pkg-config} to learn about the necessary compiler and
  88. linker flags.
  89. @example
  90. # Use GCC, if you have it installed.
  91. CC=gcc
  92. # Tell the C compiler where to find <libguile.h>
  93. CFLAGS=`pkg-config --cflags guile-@value{EFFECTIVE-VERSION}`
  94. # Tell the linker what libraries to use and where to find them.
  95. LIBS=`pkg-config --libs guile-@value{EFFECTIVE-VERSION}`
  96. simple-guile: simple-guile.o
  97. $@{CC@} simple-guile.o $@{LIBS@} -o simple-guile
  98. simple-guile.o: simple-guile.c
  99. $@{CC@} -c $@{CFLAGS@} simple-guile.c
  100. @end example
  101. If you are using the GNU Autoconf package to make your application more
  102. portable, Autoconf will settle many of the details in the Makefile above
  103. automatically, making it much simpler and more portable; we recommend
  104. using Autoconf with Guile. Here is a @file{configure.ac} file for
  105. @code{simple-guile} that uses the standard @code{PKG_CHECK_MODULES}
  106. macro to check for Guile. Autoconf will process this file into a
  107. @code{configure} script. We recommend invoking Autoconf via the
  108. @code{autoreconf} utility.
  109. @example
  110. AC_INIT(simple-guile.c)
  111. # Find a C compiler.
  112. AC_PROG_CC
  113. # Check for Guile
  114. PKG_CHECK_MODULES([GUILE], [guile-@value{EFFECTIVE-VERSION}])
  115. # Generate a Makefile, based on the results.
  116. AC_OUTPUT(Makefile)
  117. @end example
  118. Run @code{autoreconf -vif} to generate @code{configure}.
  119. Here is a @code{Makefile.in} template, from which the @code{configure}
  120. script produces a Makefile customized for the host system:
  121. @example
  122. # The configure script fills in these values.
  123. CC=@@CC@@
  124. CFLAGS=@@GUILE_CFLAGS@@
  125. LIBS=@@GUILE_LIBS@@
  126. simple-guile: simple-guile.o
  127. $@{CC@} simple-guile.o $@{LIBS@} -o simple-guile
  128. simple-guile.o: simple-guile.c
  129. $@{CC@} -c $@{CFLAGS@} simple-guile.c
  130. @end example
  131. The developer should use Autoconf to generate the @file{configure}
  132. script from the @file{configure.ac} template, and distribute
  133. @file{configure} with the application. Here's how a user might go about
  134. building the application:
  135. @example
  136. $ ls
  137. Makefile.in configure* configure.ac simple-guile.c
  138. $ ./configure
  139. checking for gcc... ccache gcc
  140. checking whether the C compiler works... yes
  141. checking for C compiler default output file name... a.out
  142. checking for suffix of executables...
  143. checking whether we are cross compiling... no
  144. checking for suffix of object files... o
  145. checking whether we are using the GNU C compiler... yes
  146. checking whether ccache gcc accepts -g... yes
  147. checking for ccache gcc option to accept ISO C89... none needed
  148. checking for pkg-config... /usr/bin/pkg-config
  149. checking pkg-config is at least version 0.9.0... yes
  150. checking for GUILE... yes
  151. configure: creating ./config.status
  152. config.status: creating Makefile
  153. $ make
  154. [...]
  155. $ ./simple-guile
  156. guile> (+ 1 2 3)
  157. 6
  158. guile> (getpwnam "jimb")
  159. #("jimb" "83Z7d75W2tyJQ" 4008 10 "Jim Blandy" "/u/jimb"
  160. "/usr/local/bin/bash")
  161. guile> (exit)
  162. $
  163. @end example
  164. @c Local Variables:
  165. @c TeX-master: "guile.texi"
  166. @c End: