libguile-linking.texi 6.6 KB

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