Re: [CM] s7 wrap c func with scheme
I just conjured up this example (9/10 of the time was spent fussing with the compiler flags...): file add1.c: #include #include "s7.h" static s7_pointer add1(s7_scheme *sc, s7_pointer args) { if (s7_is_integer(s7_car(args))) return(s7_make_integer(sc, 1 + s7_integer(s7_car(args; return(s7_wrong_type_arg_error(sc, "add1", 1, s7_car(args), "an integer")); } void add1_init(s7_scheme *sc); void add1_init(s7_scheme *sc) { s7_define_function(sc, "add1", add1, 1, 0, false, "(add1 int) adds 1 to int"); } now build it as a shared object, build s7 with -fpic, and load add1 into s7 at runtime: gcc -fpic -c add1.c gcc -shared -Wl,-soname,libadd1.so -o libadd1.so add1.o -lm -lc gcc s7.c -o repl -fpic -DWITH_MAIN -I. -ldl -lm -Wl,-export-dynamic -DUSE_SND=0 repl > (load "libadd1.so" (inlet 'init_func 'add1_init)) > (add1 2) ___ Cmdist mailing list Cmdist@ccrma.stanford.edu https://cm-mail.stanford.edu/mailman/listinfo/cmdist
Re: [CM] s7 wrap c func with scheme
Probably the simplest way to make a C function callable from s7 is to wrap it in a function of the form s7_pointer wrapper(s7_scheme *sc, s7_pointer args) Then get the scheme-side arguments from "args", and wrap the C function's output using the s7_make* functions. It is then defined in s7 using s7_define_function. s7.html has an example under "Define a function with arguments". If you want this in a shared library, you also need to tell s7's load function how to get the wrappers. This is normally done via an environment with a field named 'init_func whose value is the (name of the) C initialization function in the library (i.e. the function that calls s7_define_function). There's an example in the section about libsndlib.so, but maybe I should add a simpler example. cload.scm sets up all these wrappers and linkages for you, but it is overkill for simple cases. ___ Cmdist mailing list Cmdist@ccrma.stanford.edu https://cm-mail.stanford.edu/mailman/listinfo/cmdist
[CM] s7 wrap c func with scheme
Hi, I had a question about wrapping a c func with scheme, s7. I'm trying to understand how to wrap the declared function from the .h file with scheme in a .scm file (mywrapper.scm) so that it can be called from s7 and use the shared library. > (load mywrapper.scm) > (myfunction) > (exit) I have a .c file which includes a function (myfunction), and .h which declares that function. I compile the .c file using -fpic into an obj. file, then create a shared library, ex. mylib.so. I'm looking at SND files libc.scm and libgsl.scm which seem to be wrappers, but are quite complicated. I'm also looking at the S7 manual example for "Load a shared library", but there are static s7_pointer functions in the example's c code which seem to be doing the wrapping on the c side as opposed to wrapping a c function in scheme using a .scm file. I'm wondering if there's a simpler example somplace where I can get some ideas about how to go about wrapping a c function in scheme, or if the manual vers. is the preferred way to go, i.e. create a library using s7 functions from c, and then load the library into s7 to call the function? Regards, Jim ___ Cmdist mailing list Cmdist@ccrma.stanford.edu https://cm-mail.stanford.edu/mailman/listinfo/cmdist