libguile-linking.texi 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 --- how to start up the Guile
  59. interpreter from C code. */
  60. /* Get declarations for all the scm_ functions. */
  61. #include <libguile.h>
  62. static void
  63. inner_main (void *closure, int argc, char **argv)
  64. @{
  65. /* module initializations would go here */
  66. scm_shell (argc, argv);
  67. @}
  68. int
  69. main (int argc, char **argv)
  70. @{
  71. scm_boot_guile (argc, argv, inner_main, 0);
  72. return 0; /* never reached */
  73. @}
  74. @end example
  75. The @code{main} function calls @code{scm_boot_guile} to initialize
  76. Guile, passing it @code{inner_main}. Once @code{scm_boot_guile} is
  77. ready, it invokes @code{inner_main}, which calls @code{scm_shell} to
  78. process the command-line arguments in the usual way.
  79. Here is a Makefile which you can use to compile the above 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. If you are using the GNU Autoconf package to make your application more
  95. portable, Autoconf will settle many of the details in the Makefile above
  96. automatically, making it much simpler and more portable; we recommend
  97. using Autoconf with Guile. Here is a @file{configure.ac} file for
  98. @code{simple-guile} that uses the standard @code{PKG_CHECK_MODULES}
  99. macro to check for Guile. Autoconf will process this file into a
  100. @code{configure} script. We recommend invoking Autoconf via the
  101. @code{autoreconf} utility.
  102. @example
  103. AC_INIT(simple-guile.c)
  104. # Find a C compiler.
  105. AC_PROG_CC
  106. # Check for Guile
  107. PKG_CHECK_MODULES([GUILE], [guile-@value{EFFECTIVE-VERSION}])
  108. # Generate a Makefile, based on the results.
  109. AC_OUTPUT(Makefile)
  110. @end example
  111. Run @code{autoreconf -vif} to generate @code{configure}.
  112. Here is a @code{Makefile.in} template, from which the @code{configure}
  113. script produces a Makefile customized for the host system:
  114. @example
  115. # The configure script fills in these values.
  116. CC=@@CC@@
  117. CFLAGS=@@GUILE_CFLAGS@@
  118. LIBS=@@GUILE_LIBS@@
  119. simple-guile: simple-guile.o
  120. $@{CC@} simple-guile.o $@{LIBS@} -o simple-guile
  121. simple-guile.o: simple-guile.c
  122. $@{CC@} -c $@{CFLAGS@} simple-guile.c
  123. @end example
  124. The developer should use Autoconf to generate the @file{configure}
  125. script from the @file{configure.ac} template, and distribute
  126. @file{configure} with the application. Here's how a user might go about
  127. building the application:
  128. @example
  129. $ ls
  130. Makefile.in configure* configure.ac simple-guile.c
  131. $ ./configure
  132. checking for gcc... ccache gcc
  133. checking whether the C compiler works... yes
  134. checking for C compiler default output file name... a.out
  135. checking for suffix of executables...
  136. checking whether we are cross compiling... no
  137. checking for suffix of object files... o
  138. checking whether we are using the GNU C compiler... yes
  139. checking whether ccache gcc accepts -g... yes
  140. checking for ccache gcc option to accept ISO C89... none needed
  141. checking for pkg-config... /usr/bin/pkg-config
  142. checking pkg-config is at least version 0.9.0... yes
  143. checking for GUILE... yes
  144. configure: creating ./config.status
  145. config.status: creating Makefile
  146. $ make
  147. [...]
  148. $ ./simple-guile
  149. guile> (+ 1 2 3)
  150. 6
  151. guile> (getpwnam "jimb")
  152. #("jimb" "83Z7d75W2tyJQ" 4008 10 "Jim Blandy" "/u/jimb"
  153. "/usr/local/bin/bash")
  154. guile> (exit)
  155. $
  156. @end example
  157. @c Local Variables:
  158. @c TeX-master: "guile.texi"
  159. @c End: