1
0
Fork 0
cl-sites/guile.html_node/A-Sample-Guile-Main-Program.html
2024-12-17 12:49:28 +01:00

200 lines
7.7 KiB
HTML

<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This manual documents Guile version 3.0.10.
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
Inc.
Copyright (C) 2021 Maxime Devos
Copyright (C) 2024 Tomas Volf
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License." -->
<title>A Sample Guile Main Program (Guile Reference Manual)</title>
<meta name="description" content="A Sample Guile Main Program (Guile Reference Manual)">
<meta name="keywords" content="A Sample Guile Main Program (Guile Reference Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content=".texi2any-real">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Linking-Programs-With-Guile.html" rel="up" title="Linking Programs With Guile">
<link href="Guile-Initialization-Functions.html" rel="prev" title="Guile Initialization Functions">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="A-Sample-Guile-Main-Program">
<div class="nav-panel">
<p>
Previous: <a href="Guile-Initialization-Functions.html" accesskey="p" rel="prev">Guile Initialization Functions</a>, Up: <a href="Linking-Programs-With-Guile.html" accesskey="u" rel="up">Linking Programs With Guile</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="A-Sample-Guile-Main-Program-1"><span>5.2.2 A Sample Guile Main Program<a class="copiable-link" href="#A-Sample-Guile-Main-Program-1"> &para;</a></span></h4>
<p>Here is <samp class="file">simple-guile.c</samp>, source code for a <code class="code">main</code> and an
<code class="code">inner_main</code> function that will produce a complete Guile
interpreter.
</p>
<div class="example">
<pre class="example-preformatted">/* simple-guile.c --- Start Guile from C. */
#include &lt;libguile.h&gt;
static void
inner_main (void *closure, int argc, char **argv)
{
/* preparation */
scm_shell (argc, argv);
/* after exit */
}
int
main (int argc, char **argv)
{
scm_boot_guile (argc, argv, inner_main, 0);
return 0; /* never reached, see inner_main */
}
</pre></div>
<p>The <code class="code">main</code> function calls <code class="code">scm_boot_guile</code> to initialize
Guile, passing it <code class="code">inner_main</code>. Once <code class="code">scm_boot_guile</code> is
ready, it invokes <code class="code">inner_main</code>, which calls <code class="code">scm_shell</code> to
process the command-line arguments in the usual way.
</p>
</div>
<div class="subsection-level-extent" id="Building-the-Example-with-Make">
<h4 class="subsection"><span>5.2.3 Building the Example with Make<a class="copiable-link" href="#Building-the-Example-with-Make"> &para;</a></span></h4>
<p>Here is a Makefile which you can use to compile the example program. It
uses <code class="code">pkg-config</code> to learn about the necessary compiler and
linker flags.
</p><div class="example">
<pre class="example-preformatted"># Use GCC, if you have it installed.
CC=gcc
# Tell the C compiler where to find &lt;libguile.h&gt;
CFLAGS=`pkg-config --cflags guile-3.0`
# Tell the linker what libraries to use and where to find them.
LIBS=`pkg-config --libs guile-3.0`
simple-guile: simple-guile.o
${CC} simple-guile.o ${LIBS} -o simple-guile
simple-guile.o: simple-guile.c
${CC} -c ${CFLAGS} simple-guile.c
</pre></div>
</div>
<div class="subsection-level-extent" id="Building-the-Example-with-Autoconf">
<h4 class="subsection"><span>5.2.4 Building the Example with Autoconf<a class="copiable-link" href="#Building-the-Example-with-Autoconf"> &para;</a></span></h4>
<p>If you are using the GNU Autoconf package to make your application more
portable, Autoconf will settle many of the details in the Makefile
automatically, making it much simpler and more portable; we recommend
using Autoconf with Guile. Here is a <samp class="file">configure.ac</samp> file for
<code class="code">simple-guile</code> that uses the standard <code class="code">PKG_CHECK_MODULES</code>
macro to check for Guile. Autoconf will process this file into a
<code class="code">configure</code> script. We recommend invoking Autoconf via the
<code class="code">autoreconf</code> utility.
</p>
<div class="example">
<pre class="example-preformatted">AC_INIT(simple-guile.c)
# Find a C compiler.
AC_PROG_CC
# Check for Guile
PKG_CHECK_MODULES([GUILE], [guile-3.0])
# Generate a Makefile, based on the results.
AC_OUTPUT(Makefile)
</pre></div>
<p>Run <code class="code">autoreconf -vif</code> to generate <code class="code">configure</code>.
</p>
<p>Here is a <code class="code">Makefile.in</code> template, from which the <code class="code">configure</code>
script produces a Makefile customized for the host system:
</p><div class="example">
<pre class="example-preformatted"># The configure script fills in these values.
CC=@CC@
CFLAGS=@GUILE_CFLAGS@
LIBS=@GUILE_LIBS@
simple-guile: simple-guile.o
${CC} simple-guile.o ${LIBS} -o simple-guile
simple-guile.o: simple-guile.c
${CC} -c ${CFLAGS} simple-guile.c
</pre></div>
<p>The developer should use Autoconf to generate the <samp class="file">configure</samp>
script from the <samp class="file">configure.ac</samp> template, and distribute
<samp class="file">configure</samp> with the application. Here&rsquo;s how a user might go about
building the application:
</p>
<div class="example">
<pre class="example-preformatted">$ ls
Makefile.in configure* configure.ac simple-guile.c
$ ./configure
checking for gcc... ccache gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether ccache gcc accepts -g... yes
checking for ccache gcc option to accept ISO C89... none needed
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for GUILE... yes
configure: creating ./config.status
config.status: creating Makefile
$ make
[...]
$ ./simple-guile
guile&gt; (+ 1 2 3)
6
guile&gt; (getpwnam &quot;jimb&quot;)
#(&quot;jimb&quot; &quot;83Z7d75W2tyJQ&quot; 4008 10 &quot;Jim Blandy&quot; &quot;/u/jimb&quot;
&quot;/usr/local/bin/bash&quot;)
guile&gt; (exit)
$
</pre></div>
</div>
<hr>
<div class="nav-panel">
<p>
Previous: <a href="Guile-Initialization-Functions.html">Guile Initialization Functions</a>, Up: <a href="Linking-Programs-With-Guile.html">Linking Programs With Guile</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>