version 1.194, 2008/04/25 22:17:17
|
version 1.195, 2008/04/28 08:42:59
|
Line 12056 as well as a way of declaring structs, u
|
Line 12056 as well as a way of declaring structs, u
|
* Calling C Functions:: |
* Calling C Functions:: |
* Declaring C Functions:: |
* Declaring C Functions:: |
* Calling C function pointers:: |
* Calling C function pointers:: |
|
* Declaring libraries:: |
* Callbacks:: |
* Callbacks:: |
* C interface internals:: |
* C interface internals:: |
* Low-Level C Interface Words:: |
* Low-Level C Interface Words:: |
Line 12232 interface; you can find some examples fo
|
Line 12233 interface; you can find some examples fo
|
in @file{lib.fs}. |
in @file{lib.fs}. |
|
|
|
|
@node Calling C function pointers, Callbacks, Declaring C Functions, C Interface |
@node Calling C function pointers, Declaring libraries, Declaring C Functions, C Interface |
@subsection Calling C function pointers from Forth |
@subsection Calling C function pointers from Forth |
@cindex C function pointers, calling from Forth |
@cindex C function pointers, calling from Forth |
|
|
Line 12269 function pointer you want to call from F
|
Line 12270 function pointer you want to call from F
|
a separate calling word. |
a separate calling word. |
|
|
|
|
@node Callbacks, C interface internals, Calling C function pointers, C Interface |
@node Declaring libraries, Callbacks, Calling C function pointers, C Interface |
|
@subsection Declaring libraries |
|
@cindex Shared libraries in C interface |
|
@cindex Dynamically linked libraries in C interface |
|
@cindex Libraries in C interface |
|
|
|
For calling some C functions, you need to link with a specific library |
|
that contains that library. E.g., the @code{sin} function requires |
|
linking a special library by using the command line switch @code{-lm}. |
|
In our C iterface you do the equivalent thing by calling |
|
@code{add-lib} as follows: |
|
|
|
@example |
|
clear-libs |
|
s" m" add-lib |
|
\c #include <math.h> |
|
c-function sin sin r -- r |
|
@end example |
|
|
|
First, you clear any libraries that may have been declared earlier |
|
(you don't need them for @code{sin}); then you add the @code{m} |
|
library (actually @code{libm.so} or somesuch) to the currently |
|
declared libraries; you can add as many as you need. Finally you |
|
declare the function as shown above. Typically you will use the same |
|
set of library declarations for many function declarations; you need |
|
to write only one set for that, right at the beginning. |
|
|
|
doc-clear-libs |
|
doc-add-lib |
|
|
|
|
|
@node Callbacks, C interface internals, Declaring libraries, C Interface |
@subsection Callbacks |
@subsection Callbacks |
@cindex Callback functions written in Forth |
@cindex Callback functions written in Forth |
@cindex C function pointers to Forth words |
@cindex C function pointers to Forth words |
Line 12326 conversion.
|
Line 12358 conversion.
|
These wrapper functions are eventually compiled and dynamically linked |
These wrapper functions are eventually compiled and dynamically linked |
into Gforth, and then they can be called. |
into Gforth, and then they can be called. |
|
|
|
The libraries added with @code{add-lib} are used in the compile |
|
command line to specify dependent libraries with @code{-l@var{lib}}, |
|
causing these libraries to be dynamically linked when the wrapper |
|
function is linked. |
|
|
|
|
@node Low-Level C Interface Words, , C interface internals, C Interface |
@node Low-Level C Interface Words, , C interface internals, C Interface |
@subsection Low-Level C Interface Words |
@subsection Low-Level C Interface Words |