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