[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
29.8 A complete library example
For the means of an example let’s suppose we want to design a Bigloo
library for 2d points. That library is made of three implementation
files: two C files, cpoint.h
and cpoint.c
and one Scheme
file spoint.scm
. Here are defined the three files:
cpoint.h
:
struct point_2d { double x, y; };
cpoint.c
:
#include <stdio.h> #include "cpoint.h" int print_point_2d( struct point_2d *pt ) { printf( "<point-2d: %g, %g>", pt->x, pt->y ); }
spoint.scm
:
(module __point (include "spoint.sch") (extern (include "cpoint.h")) (export (make-point::s-point_2d* ::double ::double) (print-point ::s-point_2d*) (point? ::obj))) (define (make-point::s-point_2d* x::double y::double) (s-point_2d* x y)) (define (print-point p::s-point_2d*) (print_point_2d p)) (define (point? obj::obj) (s-point_2d*? obj) obj)
makelib.scm
:
We want our library to be composed of the whole exported Scheme functions. Thus the file to build the heap library could look like:
(module __point_makelib (import __point) (eval (export-all)))
point.init
:
Let’s suppose that the point
library requires the libposix
library. This means that any file linked with the point
library
needs to be also linked with the posix
library. Furthermore,
programs making use of the point
library needs to include the
point.sch
file. That Scheme file needs in turn the C file
point.h
otherwise the produced C files won’t compile. The need
for the libposix
library and for the point.h
file may be
specified inside the point.init
file. For our current library,
the point.init
file could look like:
(declare-library! 'point :basename "point" :srfi '(point) :eval-init '__point_makelib) (set! *ld-options* (string-append "-L/usr/lib " *ld-options*)) (set! *bigloo-user-lib* (cons "-lm" *bigloo-user-lib*)) (set! *additional-include-foreign* (cons "cpoint.h" *additional-include-foreign*)) (define-macro (point x y) `(make-point ,x ,y))
This file updates some compilation variables (*ld-options*
,
*bigloo-user-lib*
, *additional-include-foreign*
) and
defines a macro: point
. Because the point.init
file will
be loaded each time a compilation require the point
library is
spawned, user code are allowed to use the point
macro. Here is an
example file making use of the point
library:
example.scm
(module example) (let ((p (point 2.9 3.5))) (print "point?: " (point? p)) (print "point?: " (point? 4)) (print-point p) (print "done..."))
To conclude that example here is the Makefile
used to compile
the point
library, heap file and one example.
# bigloo flags BIGLOO = bigloo RELEASE = ‘$(BIGLOO) -eval ’(begin (print *bigloo-version*) (exit 0))’‘ BHEAPFLAGS = -unsafe -q -mkaddheap -mkaddlib -v2 -heap-library point BCOMMONFLAGGS = -mkaddlib -fsharing -q $(VERBOSE) \ -copt ’$(CCOMMONFLAGS)’ -cc $(CC) BSAFEFLAGS = $(BCOMMONFLAGGS) -cg -O3 -g -cg -unsafev \ -eval ’(set! *indent* 4)’ -rm BUNSAFEFLAGS = $(BCOMMONFLAGS) -O4 -unsafe # cigloo flags CIGLOO = cigloo # cflags CC = gcc CCOMMONFLAGS = -I. CSAFEFLAGS = $(CCOMMONFLAGS) CUNSAFEFLAGS = $(CCOMMONFLAGS) -O2 # library objects SAFE_OBJECT = olib/spoint.o olib/cpoint.o UNSAFE_OBJECT = olib_u/spoint.o olib_u/cpoint.o all: .afile heap lib example .afile: spoint.scm makelib.scm bglafile $^ > $@ heap: point.heap point.heap: spoint.sch spoint.scm $(BIGLOO) $(BHEAPFLAGS) makelib.scm -addheap point.heap lib: lib_u lib.a lib.a: olib $(SAFE_OBJECT) ar qcv libpoint_s-$(RELEASE).a $(SAFE_OBJECT) lib_u: olib_u $(UNSAFE_OBJECT) ar qcv libpoint_u-$(RELEASE).a $(UNSAFE_OBJECT) olib: mkdir olib olib_u: mkdir olib_u olib_u/spoint.o olib/spoint.o: spoint.scm $(BIGLOO) $(BSAFEFLAGS) $(<F) -o $*.o -c olib_u/cpoint.o olib/cpoint.o: cpoint.c $(CC) $(CSAFEFLAGS) $(<F) -o $*.o -c spoint.sch: cpoint.h cpoint.c cigloo $^ > $@ example: heap lib $(BIGLOO) -v2 -L . -library point \ -static-bigloo example.scm -o example clean: -/bin/rm -f point.heap -/bin/rm -f spoint.sch spoint.c -/bin/rm -fr olib olib_u -/bin/rm -f example example.c example.o -/bin/rm -f libpoint_s-$(RELEASE).a libpoint_u-$(RELEASE).a
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.