[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Embedded C Question
From: |
Thomas Bushnell BSG |
Subject: |
Re: [Chicken-users] Embedded C Question |
Date: |
Tue, 05 Jan 2010 16:02:52 -0800 |
The FFI facilities are only available in compiled code. If you want to
use them in interpreted code, package the FFI bits into a separate file,
compile it with -s, and then you can load that into the interpreter.
Thomas
On Mon, 2010-01-04 at 17:33 -0500, David N Murray wrote:
> Hi all,
>
> I've been toying around with embedded C just to learn how. I have the
> following sample:
>
> (use easyffi)
> (import foreign)
>
> #>
> #include <stdio.h>
> #include <time.h>
>
> const char* dummy(char* dst, int len, int seconds) {
> struct tm* tm = localtime(&seconds);
> snprintf(dst, len, "%02d/%02d:%02d:%02d:%02d:", tm->tm_mon + 1,
> tm->tm_mday, tm->tm_hour,tm->tm_min, tm->tm_sec);
> return dst;
> }
> <#
>
> (define c-dummy (foreign-lambda c-string "dummy" c-string
> integer integer))
>
> (define (test-dummy)
> (c-dummy (make-string 128) 128 1242403665))
>
> (print (bar->string))
>
> It runs fine this way (actually, it also runs without the first two
> lines):
>
> $ csc t2.scm
>
> Warning: re-importing already imported syntax: foreign-declare
>
> Warning: re-importing already imported syntax: foreign-declare
> /usr/local/lib/libchicken.so: warning: strcpy() is almost always misused,
> please use strlcpy()
> /usr/local/lib/libchicken.so: warning: sprintf() is often misused, please
> use snprintf()
> /usr/local/lib/libchicken.so: warning: strcat() is almost always misused,
> please use strlcat()
> $ ./t2
> 05/15:12:07:45:
> $
>
> However, when I try and run it in csi, I get the following:
> $ csi t2.scm
>
> CHICKEN
> (c)2008-2009 The Chicken Team
> (c)2000-2007 Felix L. Winkelmann
> Version 4.3.0
> openbsd-unix-gnu-x86-64 [ 64bit manyargs dload ptables ]
> compiled 2010-01-04 on kili.jsbsystems.com (OpenBSD)
>
> ; loading ~/.csirc ...
> ; loading /usr/local/lib/chicken/4/readline.import.so ...
> ; loading /usr/local/lib/chicken/4/scheme.import.so ...
> ; loading /usr/local/lib/chicken/4/chicken.import.so ...
> ; loading /usr/local/lib/chicken/4/foreign.import.so ...
> ; loading /usr/local/lib/chicken/4/ports.import.so ...
> ; loading /usr/local/lib/chicken/4/data-structures.import.so ...
> ; loading /usr/local/lib/chicken/4/posix.import.so ...
> ; loading /usr/local/lib/chicken/4/readline.so ...
> ; loading t2.scm ...
> ; loading /usr/local/lib/chicken/4/easyffi.so ...
> ; loading /usr/local/lib/chicken/4/easyffi-base.so ...
> ; loading /usr/local/lib/chicken/4/easyffi.import.so ...
>
> Warning: declarations are ignored in interpreted code: (##core#declare
> (foreign-declare "\n#define _X_OPEN_SOURCE\n#include <stdio.h>\n#include
> <time.h>\n\nconst char* bar2string(char* dst, int len, int seconds) {\n
> struct tm* tm = localtime(&seconds);\n snprintf(dst, len,
> \"%02d/%02d:%02d:%02d:%02d:\", tm->tm_mon + 1, \n\t\t tm->tm_mday,
> tm->tm_hour,tm->tm_min, tm->tm_sec);\n return dst;\n}\n" "\n"))
>
> Error: unbound variable: foreign-lambda
>
> Call history:
>
> g626627
> action383
> <syntax> (##core#declare (foreign-declare
> "\n#define _X_OPEN_SOURCE\n#include <stdio.h>\n#include <time.h>\n\...
> <syntax> (##core#undefined)
> <syntax> (define c-bar2string (foreign-lambda
> c-string "bar2string" c-string integer integer))
> <syntax> (##core#set! c-bar2string (foreign-lambda
> c-string "bar2string" c-string integer integer))
> <syntax> (foreign-lambda c-string "bar2string"
> c-string integer integer)
> <eval> (foreign-lambda c-string "bar2string" c-string
> integer integer) <--
> $
>
> What am I missing?
>
> TIA,
> Dave
>
>
>
> _______________________________________________
> Chicken-users mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/chicken-users