Commit | Line | Data |
---|---|---|
3229f68b MV |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Guile Reference Manual. | |
1ea8aa7d | 3 | @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2010 |
3229f68b MV |
4 | @c Free Software Foundation, Inc. |
5 | @c See the file guile.texi for copying conditions. | |
6 | ||
7 | @node Linking Programs With Guile | |
8 | @section Linking Programs With Guile | |
9 | ||
10 | This section covers the mechanics of linking your program with Guile | |
11 | on a typical POSIX system. | |
12 | ||
13 | The header file @code{<libguile.h>} provides declarations for all of | |
14 | Guile's functions and constants. You should @code{#include} it at the | |
15 | head of any C source file that uses identifiers described in this | |
16 | manual. Once you've compiled your source files, you need to link them | |
17 | against the Guile object code library, @code{libguile}. | |
18 | ||
19 | On most systems, you should not need to tell the compiler and linker | |
20 | explicitly where they can find @file{libguile.h} and @file{libguile}. | |
21 | When Guile has been installed in a peculiar way, or when you are on a | |
22 | peculiar system, things might not be so easy and you might need to pass | |
23 | additional @code{-I} or @code{-L} options to the compiler. Guile | |
24 | provides the utility program @code{guile-config} to help you find the | |
25 | right values for these options. You would typically run | |
26 | @code{guile-config} during the configuration phase of your program and | |
27 | use the obtained information in the Makefile. | |
28 | ||
29 | @menu | |
30 | * Guile Initialization Functions:: What to call first. | |
31 | * A Sample Guile Main Program:: Sources and makefiles. | |
32 | @end menu | |
33 | ||
34 | ||
35 | @node Guile Initialization Functions | |
36 | @subsection Guile Initialization Functions | |
37 | ||
beac6039 MV |
38 | To initialize Guile, you can use one of several functions. The first, |
39 | @code{scm_with_guile}, is the most portable way to initialize Guile. It | |
40 | will initialize Guile when necessary and then call a function that you | |
41 | can specify. Multiple threads can call @code{scm_with_guile} | |
42 | concurrently and it can also be called more than once in a given thread. | |
43 | The global state of Guile will survive from one call of | |
44 | @code{scm_with_guile} to the next. Your function is called from within | |
45 | @code{scm_with_guile} since the garbage collector of Guile needs to know | |
46 | where the stack of each thread is. | |
47 | ||
48 | A second function, @code{scm_init_guile}, initializes Guile for the | |
49 | current thread. When it returns, you can use the Guile API in the | |
50 | current thread. This function employs some non-portable magic to learn | |
51 | about stack bounds and might thus not be available on all platforms. | |
3229f68b MV |
52 | |
53 | One common way to use Guile is to write a set of C functions which | |
54 | perform some useful task, make them callable from Scheme, and then link | |
55 | the program with Guile. This yields a Scheme interpreter just like | |
56 | @code{guile}, but augmented with extra functions for some specific | |
57 | application --- a special-purpose scripting language. | |
58 | ||
59 | In this situation, the application should probably process its | |
60 | command-line arguments in the same manner as the stock Guile | |
61 | interpreter. To make that straightforward, Guile provides the | |
beac6039 | 62 | @code{scm_boot_guile} and @code{scm_shell} function. |
3229f68b | 63 | |
871d8543 NJ |
64 | For more about these functions, see @ref{Initialization}. |
65 | ||
3229f68b MV |
66 | @node A Sample Guile Main Program |
67 | @subsection A Sample Guile Main Program | |
68 | ||
69 | Here is @file{simple-guile.c}, source code for a @code{main} and an | |
70 | @code{inner_main} function that will produce a complete Guile | |
71 | interpreter. | |
72 | ||
73 | @example | |
74 | /* simple-guile.c --- how to start up the Guile | |
75 | interpreter from C code. */ | |
76 | ||
77 | /* Get declarations for all the scm_ functions. */ | |
78 | #include <libguile.h> | |
79 | ||
80 | static void | |
81 | inner_main (void *closure, int argc, char **argv) | |
82 | @{ | |
83 | /* module initializations would go here */ | |
84 | scm_shell (argc, argv); | |
85 | @} | |
86 | ||
87 | int | |
88 | main (int argc, char **argv) | |
89 | @{ | |
90 | scm_boot_guile (argc, argv, inner_main, 0); | |
91 | return 0; /* never reached */ | |
92 | @} | |
93 | @end example | |
94 | ||
95 | The @code{main} function calls @code{scm_boot_guile} to initialize | |
96 | Guile, passing it @code{inner_main}. Once @code{scm_boot_guile} is | |
97 | ready, it invokes @code{inner_main}, which calls @code{scm_shell} to | |
98 | process the command-line arguments in the usual way. | |
99 | ||
100 | Here is a Makefile which you can use to compile the above program. It | |
101 | uses @code{guile-config} to learn about the necessary compiler and | |
102 | linker flags. | |
103 | @example | |
104 | # Use GCC, if you have it installed. | |
105 | CC=gcc | |
106 | ||
107 | # Tell the C compiler where to find <libguile.h> | |
108 | CFLAGS=`guile-config compile` | |
109 | ||
110 | # Tell the linker what libraries to use and where to find them. | |
111 | LIBS=`guile-config link` | |
112 | ||
113 | simple-guile: simple-guile.o | |
114 | $@{CC@} simple-guile.o $@{LIBS@} -o simple-guile | |
115 | ||
116 | simple-guile.o: simple-guile.c | |
117 | $@{CC@} -c $@{CFLAGS@} simple-guile.c | |
118 | @end example | |
119 | ||
120 | If you are using the GNU Autoconf package to make your application more | |
121 | portable, Autoconf will settle many of the details in the Makefile above | |
122 | automatically, making it much simpler and more portable; we recommend | |
123 | using Autoconf with Guile. Guile also provides the @code{GUILE_FLAGS} | |
124 | macro for autoconf that performs all necessary checks. Here is a | |
125 | @file{configure.in} file for @code{simple-guile} that uses this macro. | |
8c3fa3e5 | 126 | Autoconf can use this file as a template to generate a @code{configure} |
3229f68b | 127 | script. In order for Autoconf to find the @code{GUILE_FLAGS} macro, you |
39067a9f KR |
128 | will need to run @code{aclocal} first (@pxref{Invoking aclocal,,, |
129 | automake, GNU Automake}). | |
130 | ||
3229f68b MV |
131 | @example |
132 | AC_INIT(simple-guile.c) | |
133 | ||
134 | # Find a C compiler. | |
135 | AC_PROG_CC | |
136 | ||
137 | # Check for Guile | |
138 | GUILE_FLAGS | |
139 | ||
140 | # Generate a Makefile, based on the results. | |
141 | AC_OUTPUT(Makefile) | |
142 | @end example | |
143 | ||
144 | Here is a @code{Makefile.in} template, from which the @code{configure} | |
145 | script produces a Makefile customized for the host system: | |
146 | @example | |
147 | # The configure script fills in these values. | |
148 | CC=@@CC@@ | |
149 | CFLAGS=@@GUILE_CFLAGS@@ | |
150 | LIBS=@@GUILE_LDFLAGS@@ | |
151 | ||
152 | simple-guile: simple-guile.o | |
153 | $@{CC@} simple-guile.o $@{LIBS@} -o simple-guile | |
154 | simple-guile.o: simple-guile.c | |
155 | $@{CC@} -c $@{CFLAGS@} simple-guile.c | |
156 | @end example | |
157 | ||
158 | The developer should use Autoconf to generate the @file{configure} | |
159 | script from the @file{configure.in} template, and distribute | |
160 | @file{configure} with the application. Here's how a user might go about | |
161 | building the application: | |
162 | ||
163 | @example | |
164 | $ ls | |
165 | Makefile.in configure* configure.in simple-guile.c | |
166 | $ ./configure | |
167 | creating cache ./config.cache | |
168 | checking for gcc... (cached) gcc | |
169 | checking whether the C compiler (gcc ) works... yes | |
170 | checking whether the C compiler (gcc ) is a cross-compiler... no | |
171 | checking whether we are using GNU C... (cached) yes | |
172 | checking whether gcc accepts -g... (cached) yes | |
173 | checking for Guile... yes | |
174 | creating ./config.status | |
175 | creating Makefile | |
176 | $ make | |
1ea8aa7d | 177 | [...] |
3229f68b MV |
178 | $ ./simple-guile |
179 | guile> (+ 1 2 3) | |
180 | 6 | |
181 | guile> (getpwnam "jimb") | |
182 | #("jimb" "83Z7d75W2tyJQ" 4008 10 "Jim Blandy" "/u/jimb" | |
183 | "/usr/local/bin/bash") | |
184 | guile> (exit) | |
185 | $ | |
186 | @end example | |
187 | ||
39067a9f KR |
188 | |
189 | @c Local Variables: | |
190 | @c TeX-master: "guile.texi" | |
191 | @c End: |