[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Building a shared library
From: |
Felix |
Subject: |
Re: [Chicken-users] Building a shared library |
Date: |
Tue, 15 May 2012 10:07:11 +0200 (CEST) |
From: John Maxwell <address@hidden>
Subject: [Chicken-users] Building a shared library
Date: Mon, 14 May 2012 14:22:03 -0400
> I am trying to build a shared library, and having puzzling problems.
> I've constructed a toy example that illustrates the problem:
>
> address@hidden ~/chicken-example $ cat a.scm
> (declare (unit a))
>
> (define (a) "result a")
> address@hidden ~/chicken-example $ cat b.scm
> (declare (unit b))
>
> (define (b) "result b")
A compiled piece of code always needs to have an entry point, a C
function that will be invoked on loading the binary and which
initializes the toplevel variables and runs the toplevel Scheme code
in the file. A "unit" in CHICKEN means basically that the code has an
explicitly named entry point function with a name constructed from the
unit-name (e.g. "C_<blabla>_toplevel"). If you load a compiled
binary, the runtime system must be aware of the name to be able to
initialize the loaded code - usually this is "C_toplevel": no unit
name is given (this is also the entry-point name for non-libraries, in
other words: executable programs). Compiling with "csc -s" will
produce an .so, which has the mentionen "C_toplevel" entry
point. CHICKEN "units" on the other hand have multiple entry-points,
because each linked (C) module needs to be found and called. When
linking multiple units this is necessary and "unit" and "uses"
declarations provide a means to distinguish the entry-points of the
various compiled modules ("module" in the sense of "compilation
unit").
The scenario here is that you want to link separate "modules" into a
single shared binary, so you have multiple toplevels with multiple
entry-points that have to be called (done by the "uses" declaration).
So what to do? Provide an "anonymous" toplevel, from which the
units will be initialized:
; x.scm:
(print 1)
--
; y.scm:
(print 2)
--
; z.scm:
(print 3)
--
csc -unit x x.scm -c -s # unit, produce .o, compile for allow dynamic loading
csc -unit y y.scm -c -s # the same
csc -uses x -uses y z.scm -s x.o y.o # z is _not_ a unit and provides default
entry-point
x:~/tmp % csi
...
#;1> ,l z.so
; loading z.so ...
1
2
3
Im hope this clears things up a bit.
cheers,
felix