libguile-linking.texi 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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
  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. On most systems, you should not need to tell the compiler and linker
  16. explicitly where they can find @file{libguile.h} and @file{libguile}.
  17. When Guile has been installed in a peculiar way, or when you are on a
  18. peculiar system, things might not be so easy and you might need to pass
  19. additional @code{-I} or @code{-L} options to the compiler. Guile
  20. provides the utility program @code{guile-config} to help you find the
  21. right values for these options. You would typically run
  22. @code{guile-config} during the configuration phase of your program and
  23. use the obtained information in the Makefile.
  24. @menu
  25. * Guile Initialization Functions:: What to call first.
  26. * A Sample Guile Main Program:: Sources and makefiles.
  27. @end menu
  28. @node Guile Initialization Functions
  29. @subsection Guile Initialization Functions
  30. To initialize Guile, you can use one of several functions. The first,
  31. @code{scm_with_guile}, is the most portable way to initialize Guile. It
  32. will initialize Guile when necessary and then call a function that you
  33. can specify. Multiple threads can call @code{scm_with_guile}
  34. concurrently and it can also be called more than once in a given thread.
  35. The global state of Guile will survive from one call of
  36. @code{scm_with_guile} to the next. Your function is called from within
  37. @code{scm_with_guile} since the garbage collector of Guile needs to know
  38. where the stack of each thread is.
  39. A second function, @code{scm_init_guile}, initializes Guile for the
  40. current thread. When it returns, you can use the Guile API in the
  41. current thread. This function employs some non-portable magic to learn
  42. about stack bounds and might thus not be available on all platforms.
  43. One common way to use Guile is to write a set of C functions which
  44. perform some useful task, make them callable from Scheme, and then link
  45. the program with Guile. This yields a Scheme interpreter just like
  46. @code{guile}, but augmented with extra functions for some specific
  47. application --- a special-purpose scripting language.
  48. In this situation, the application should probably process its
  49. command-line arguments in the same manner as the stock Guile
  50. interpreter. To make that straightforward, Guile provides the
  51. @code{scm_boot_guile} and @code{scm_shell} function.
  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{guile-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=`guile-config compile`
  87. # Tell the linker what libraries to use and where to find them.
  88. LIBS=`guile-config link`
  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. Guile also provides the @code{GUILE_FLAGS}
  98. macro for autoconf that performs all necessary checks. Here is a
  99. @file{configure.in} file for @code{simple-guile} that uses this macro.
  100. Autoconf can use this file as a template to generate a @code{configure}
  101. script. In order for Autoconf to find the @code{GUILE_FLAGS} macro, you
  102. will need to run @code{aclocal} first (@pxref{Invoking aclocal,,,
  103. automake, GNU Automake}).
  104. @example
  105. AC_INIT(simple-guile.c)
  106. # Find a C compiler.
  107. AC_PROG_CC
  108. # Check for Guile
  109. GUILE_FLAGS
  110. # Generate a Makefile, based on the results.
  111. AC_OUTPUT(Makefile)
  112. @end example
  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_LDFLAGS@@
  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.in} 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.in simple-guile.c
  132. $ ./configure
  133. creating cache ./config.cache
  134. checking for gcc... (cached) gcc
  135. checking whether the C compiler (gcc ) works... yes
  136. checking whether the C compiler (gcc ) is a cross-compiler... no
  137. checking whether we are using GNU C... (cached) yes
  138. checking whether gcc accepts -g... (cached) yes
  139. checking for Guile... yes
  140. creating ./config.status
  141. creating Makefile
  142. $ make
  143. gcc -c -I/usr/local/include simple-guile.c
  144. gcc simple-guile.o -L/usr/local/lib -lguile -lqthreads -lpthread -lm -o simple-guile
  145. $ ./simple-guile
  146. guile> (+ 1 2 3)
  147. 6
  148. guile> (getpwnam "jimb")
  149. #("jimb" "83Z7d75W2tyJQ" 4008 10 "Jim Blandy" "/u/jimb"
  150. "/usr/local/bin/bash")
  151. guile> (exit)
  152. $
  153. @end example
  154. @c Local Variables:
  155. @c TeX-master: "guile.texi"
  156. @c End: