Re: gauche-c-wrapper's cwcompile stopped working: function is not found

Shiro Kawai <[email protected]> Mon, 29 Apr 2024 07:09:03 -1000
Newsgroups gmane.lisp.scheme.gauche
Message-ID <CALN0JNF5MEuefv-mWtG1xekcHk2euiosCqhBHvN-iS6UBiC2ug@mail.gmail.com>
Ok, I tried to reproduce it.  I started from Debian source (original
tarball and patches in https://packages.debian.org/buster/gauche-c-wrapper
), needed to fix a few places to compile recent Gauche.  But once it
compiles, it seems to work with your example.

The error message you got is mysterious, for it is an error when dlsym()
fails to find the symbol.

Here's the changes I made.  The change of Scm_ReadStringLiteral() is to
avoid conflict with Gauche API with the same name.

=====
diff --git a/gauche-c-wrapper-0.6.1.orig/src/c-ffi.c
b/gauche-c-wrapper-0.6.1.updated/src/c-ffi.c
index 5f99d6b..8b7834b 100644
--- a/gauche-c-wrapper-0.6.1.orig/src/c-ffi.c
+++ b/gauche-c-wrapper-0.6.1.updated/src/c-ffi.c
@@ -347,7 +347,7 @@ static ffi_type *ffi_type_of(ScmObj ctype)
     }
     sa = SCM_SLOT_ACCESSOR(SCM_CDR(p));
     if (0 <= sa->slotNumber) {
-        return SCM_FFI_TYPE_DATA(Scm_InstanceSlotRef(ctype,
sa->slotNumber));
+        return SCM_FFI_TYPE_DATA(Scm_InstanceSlotRef(ctype,
sa->slotNumber, SCM_UNDEFINED));
     } else {
         Scm_Error("wrong slot number: %d", sa->slotNumber);
     }
diff --git a/gauche-c-wrapper-0.6.1.orig/src/c-lex.c
b/gauche-c-wrapper-0.6.1.updated/src/c-lex.c
index 546a469..bab0a08 100644
--- a/gauche-c-wrapper-0.6.1.orig/src/c-lex.c
+++ b/gauche-c-wrapper-0.6.1.updated/src/c-lex.c
@@ -992,7 +992,7 @@ static ScmObj read_character_constant()
     SCM_RETURN(Scm_MakeInteger(v));
 }

-ScmObj Scm_ReadStringLiteral()
+ScmObj Scm_ReadCStringLiteral()
 {
     SCM_RETURN(read_string_literal());
 }
diff --git a/gauche-c-wrapper-0.6.1.orig/src/c-lex.h
b/gauche-c-wrapper-0.6.1.updated/src/c-lex.h
index 47b3f15..3ce93fc 100644
--- a/gauche-c-wrapper-0.6.1.orig/src/c-lex.h
+++ b/gauche-c-wrapper-0.6.1.updated/src/c-lex.h
@@ -78,7 +78,7 @@ extern ScmObj Scm_IsForceIdentifierRef();
 extern ScmObj Scm_IsForceIdentifierSet(ScmObj v);
 extern ScmObj Scm_ReadIdentifier(ScmObj l);
 extern ScmObj Scm_ReadOperator(ScmObj c);
-extern ScmObj Scm_ReadStringLiteral();
+extern ScmObj Scm_ReadCStringLiteral();
 extern ScmObj Scm_ReadCharacterConstant();
 extern ScmObj Scm_ReadOctalOrFlonum(ScmObj l);
 extern ScmObj Scm_ReadDecimal(ScmObj l);
diff --git a/gauche-c-wrapper-0.6.1.orig/src/c-lexlib.stub
b/gauche-c-wrapper-0.6.1.updated/src/c-lexlib.stub
index 4138274..c70d7ed 100644
--- a/gauche-c-wrapper-0.6.1.orig/src/c-lexlib.stub
+++ b/gauche-c-wrapper-0.6.1.updated/src/c-lexlib.stub
@@ -40,7 +40,7 @@
         "if (nptr == endptr) {"
         "    SCM_RETURN(SCM_FALSE);"
         "} else {"
-        "    SCM_RETURN(Scm_MakeInteger64((ScmInt64) v));"
+        "    SCM_RETURN(Scm_MakeInteger64((int64_t) v));"
         "}"))

 (define-cproc strtod (nptr::<const-cstring>)
@@ -137,7 +137,7 @@
   (call "Scm_ReadOperator"))

 (define-cproc read-string-literal ()
-  (call "Scm_ReadStringLiteral"))
+  (call "Scm_ReadCStringLiteral"))

 (define-cproc read-character-constant ()
   (call "Scm_ReadCharacterConstant"))
diff --git a/gauche-c-wrapper-0.6.1.orig/src/c-parser.c
b/gauche-c-wrapper-0.6.1.updated/src/c-parser.c
index 08c39dd..d221d30 100644
--- a/gauche-c-wrapper-0.6.1.orig/src/c-parser.c
+++ b/gauche-c-wrapper-0.6.1.updated/src/c-parser.c
@@ -1691,7 +1691,7 @@ ScmObj Scm_ParseMacroCode(ScmObj in, ScmObj
macro_list)
         /* skip lines starting with "# [0-9]+ <stdin>" and join with
following line */
         while (!SCM_EOFP(next_line_str)
                && (SCM_STRING_LENGTH(next_line_str) > 2)
-               && SCM_REGMATCHP(Scm_RegExec(SCM_REGEXP(skip_regex),
SCM_STRING(next_line_str)))) {
+               && SCM_REGMATCHP(Scm_RegExec(SCM_REGEXP(skip_regex),
SCM_STRING(next_line_str), SCM_UNDEFINED, SCM_UNDEFINED))) {
             next_line_str = Scm_ReadLineUnsafe(SCM_PORT(in));
             if (!SCM_EOFP(next_line_str)) {
                 line_str = Scm_StringAppend2(SCM_STRING(line_str),
SCM_STRING(next_line_str));





On Sun, Apr 28, 2024 at 7:07 AM Jens Thiele <[email protected]> wrote:

> Hi,
>
> likely nobody can help (I think I am the only remaining user of gauche
> c-wrapper?) but anyway here we go:
>
> gauche-c-wrapper's cwcompile stopped working with newer gauche versions
> and so far I have no idea what's the problem. Maybe the stub file?
>
> A simple test script to create a simple shared library with an add
> function that should be used via gauche c-wrapper:
>
> #!/bin/bash -xe
> cat <<EOF > libhello.h
> int add(int i, int j);
> EOF
> cat <<EOF > libhello.c
> #include "libhello.h"
> int add(int i, int j) {
>   return i+j;
> }
> EOF
> gcc -shared -o libhello.so -fPIC libhello.c
> cat <<'EOF' > test-libhello.scm
> #!/bin/sh
> #| -*- mode: scheme; coding: utf-8; -*-
> exec gosh -I. -- $0 "$@"
> |#
> (use c-wrapper)
> (c-load "libhello.h"
>         :libs "-L. -lhello"
>         :import '(add)
>         :compiled-lib "libgauche-hello.so"
>         )
>
> (define (main args)
>   #?=(add 10 32)
>   0)
> EOF
> chmod +x ./test-libhello.scm
> rm -vf libgauche-hello.so
> gosh -V
> dpkg -l gauche
> dpkg -l gauche-c-wrapper
> ./test-libhello.scm
> cwcompile -v test-libhello.scm
> cat libgauche-hello.stub
> LD_LIBRARY_PATH=. ./test-libhello.scm
>
> output on a old system with gauche 0.9.6 where it still works:
>
> $ ./test.sh
> + cat
> + cat
> + gcc -shared -o libhello.so -fPIC libhello.c
> + cat
> + chmod +x ./test-libhello.scm
> + rm -vf libgauche-hello.so
> + gosh -V
> Gauche scheme shell, version 0.9.6 [utf-8,pthreads], x86_64-pc-linux-gnu
> + dpkg -l gauche
> Desired=Unknown/Install/Remove/Purge/Hold
> |
> Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
> |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
> ||/ Name           Version      Architecture Description
>
> +++-==============-============-============-=================================
> ii  gauche         0.9.6-10     amd64        Scheme script engine
> + dpkg -l gauche-c-wrapper
> Desired=Unknown/Install/Remove/Purge/Hold
> |
> Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
> |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
> ||/ Name             Version      Architecture Description
>
> +++-================-============-============-====================================================
> ii  gauche-c-wrapper 0.6.1-11     amd64        Foreign function interface
> for Gauche to C libraries
> + ./test-libhello.scm
> #?="././test-libhello.scm":13:(add 10 32)
> #?-    42
> + cwcompile -v test-libhello.scm
> gauche-package compile --verbose --cflags=  --cppflags=   --ldflags=
>  --libs=  -L. -lhello  libgauche-hello libgauche-hello.stub
> gcc -c    '-I/usr/lib/gauche-0.9/0.9.6/include'
> '-I/usr/lib/gauche-0.9/site/include'   -fPIC -o 'libgauche-hello.o'
> 'libgauche-hello.c'
> gcc    '-L/usr/lib/gauche-0.9/0.9.6/x86_64-pc-linux-gnu'
> '-L/usr/lib/gauche-0.9/site/x86_64-pc-linux-gnu'  -shared -o
> libgauche-hello.so 'libgauche-hello.o' -lgauche-0.9 -ldl -lcrypt -lrt -lm
> -lpthread   -L. -lhello
> + cat libgauche-hello.stub
> ;;;
> ;;; libgauche-hello.stub
> ;;;
> "#include \"stdlib.h\""
> "#include \"gauche.h\""
> "#include \"gauche/extend.h\""
> "#include \"gauche/uvector.h\""
> "#include \"libhello.h\""
> "/* Hack for initialization stub */"
> "static void internal_init(ScmModule*);"
> "static ScmObj __c_wrapper_mod;"
> "static ScmObj __sandbox_mod;"
> "void Scm_Init_libgauche_hello(void)"
> "{"
> "    SCM_INIT_EXTENSION(libgauche_hello);"
> "    __c_wrapper_mod =
> Scm_ApplyRec0(Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper.c-ffi\",
> FALSE), SCM_SYMBOL(SCM_INTERN(\"ffi-sandbox-module\")), FALSE));"
> "    __sandbox_mod =
> Scm_ApplyRec0(Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper\", FALSE),
> SCM_SYMBOL(SCM_INTERN(\"sandbox-module\")), FALSE));"
> "    internal_init(SCM_MODULE(__c_wrapper_mod));"
> "}"
> "#define Scm_Init_libgauche_hello internal_init"
> "#define INIT_SYMVAL(var, sym) if (!var) { var =
> Scm_GlobalVariableRef(SCM_MODULE(__c_wrapper_mod),
> SCM_SYMBOL(SCM_INTERN(sym)), FALSE); }"
> "static ScmObj cw_boxvar(ScmObj klass, void *varptr)"
> "{"
> "    static ScmObj make_proc = NULL;"
> "    static ScmObj csizeof_proc = NULL;"
> "    ScmObj obj;"
> "    ScmObj buf;"
> "    size_t size;"
> "    INIT_SYMVAL(make_proc, \"make\");"
> "    INIT_SYMVAL(csizeof_proc, \"c-sizeof\");"
> "    size = Scm_GetIntegerU(Scm_ApplyRec1(csizeof_proc, klass));"
> "    buf = Scm_MakeU8VectorFromArrayShared(size, varptr);"
> "    obj = Scm_ApplyRec3(make_proc, klass, SCM_MAKE_KEYWORD(\"buffer\"),
> buf);"
> "    SCM_RETURN(obj);"
> "}"
> "static void cw_unbox(void *dest, ScmObj obj, size_t size)"
> "{"
> "    static ScmObj bufferof_proc = NULL;"
> "    ScmObj buf;"
> "    if (!bufferof_proc) {"
> "        bufferof_proc =
> Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper.c-ffi\", FALSE),
> SCM_SYMBOL(SCM_INTERN(\"buffer-of\")), FALSE);"
> "    }"
> "    buf = Scm_ApplyRec1(bufferof_proc, obj);"
> "    memcpy(dest, SCM_UVECTOR_ELEMENTS(buf), size);"
> "}"
> "static ScmObj __c_wrapper_979;    /* add */"
> (define-symbol make-c-func "__c_wrapper_974")
> (define-symbol quote "__c_wrapper_975")
> (define-symbol add "__c_wrapper_976")
> (define-symbol <c-int> "__c_wrapper_977")
> (define-symbol list "__c_wrapper_978")
> (initcode "__c_wrapper_979 = Scm_EvalRec(SCM_LIST4(__c_wrapper_974,
> SCM_LIST2(__c_wrapper_975, __c_wrapper_976), __c_wrapper_977,
> SCM_LIST3(__c_wrapper_978, __c_wrapper_977, __c_wrapper_977)),
> SCM_OBJ(__c_wrapper_mod));")
> (initcode "Scm_Define(SCM_MODULE(__c_wrapper_mod),
> SCM_SYMBOL(__c_wrapper_976), __c_wrapper_979);")
> (initcode "Scm_Define(SCM_MODULE(__sandbox_mod),
> SCM_SYMBOL(__c_wrapper_976),
> Scm_GlobalVariableRef(SCM_MODULE(__c_wrapper_mod),
> SCM_SYMBOL(__c_wrapper_976), 0));")
> ;; Local variables:
> ;; mode: scheme
> ;; end:
> + LD_LIBRARY_PATH=.
> + ./test-libhello.scm
> #?="././test-libhello.scm":13:(add 10 32)
> #?-    42
>
> output on a newer system (gauche 0.9.14) where it doesn't work:
>
> $ ./test-libhello.sh
> + cat
> + cat
> + gcc -shared -o libhello.so -fPIC libhello.c
> + cat
> + chmod +x ./test-libhello.scm
> + rm -vf libgauche-hello.so
> 'libgauche-hello.so' wurde entfernt
> + gosh -V
> Gauche scheme shell, version 0.9.14 [utf-8,pthreads], x86_64-pc-linux-gnu
> (version "0.9.14")
> (command "gosh")
> (scheme.id gauche)
> (languages scheme r5rs r7rs)
> (encodings utf-8)
> (website "https://practical-scheme.net/gauche")
> (build.platform "x86_64-pc-linux-gnu")
> (build.configure)
> (scheme.path "/usr/share/gauche-0.98/site/lib"
> "/usr/share/gauche-0.98/0.9.14/lib")
> (threads pthreads)
> (gauche.net.tls mbedtls)
> + dpkg -l gauche
>
> Gewünscht=Unbekannt/Installieren/R=Entfernen/P=Vollständig Löschen/Halten
> | Status=Nicht/Installiert/Config/U=Entpackt/halb konFiguriert/
>          Halb installiert/Trigger erWartet/Trigger anhängig
> |/ Fehler?=(kein)/R=Neuinstallation notwendig (Status, Fehler:
> GROSS=schlecht)
> ||/ Name           Version            Architektur  Beschreibung
>
> +++-==============-==================-============-=================================
> ii  gauche         0.9.14-5~bookworm3 amd64        Scheme script engine
> + dpkg -l gauche-c-wrapper
>
> Gewünscht=Unbekannt/Installieren/R=Entfernen/P=Vollständig Löschen/Halten
> | Status=Nicht/Installiert/Config/U=Entpackt/halb konFiguriert/
>          Halb installiert/Trigger erWartet/Trigger anhängig
> |/ Fehler?=(kein)/R=Neuinstallation notwendig (Status, Fehler:
> GROSS=schlecht)
> ||/ Name             Version            Architektur  Beschreibung
>
> +++-================-==================-============-====================================================
> ii  gauche-c-wrapper 0.6.1-16~bookworm1 amd64        Foreign function
> interface for Gauche to C libraries
> + ./test-libhello.scm
> #?="././test-libhello.scm":13:(add 10 32)
> #?-    42
> + cwcompile -v test-libhello.scm
> gauche-package compile --verbose --cflags=  --cppflags=   --ldflags=
>  --libs=  -L. -lhello  libgauche-hello libgauche-hello.stub
> gcc -c    '-I/usr/lib/gauche-0.98/0.9.14/include'
> '-I/usr/lib/gauche-0.98/site/include'   -fPIC -o 'libgauche-hello.o'
> 'libgauche-hello.c'
> gcc    '-L/usr/lib/gauche-0.98/0.9.14/x86_64-pc-linux-gnu'
> '-L/usr/lib/gauche-0.98/site/x86_64-pc-linux-gnu'  -shared -o
> libgauche-hello.so 'libgauche-hello.o' -lgauche-0.98 -lmbedtls -lcrypt -lrt
> -lm  -lpthread   -L. -lhello
> + cat libgauche-hello.stub
> ;;;
> ;;; libgauche-hello.stub
> ;;;
> "#include \"stdlib.h\""
> "#include \"gauche.h\""
> "#include \"gauche/extend.h\""
> "#include \"gauche/uvector.h\""
> "#include \"libhello.h\""
> "/* Hack for initialization stub */"
> "static void internal_init(ScmModule*);"
> "static ScmObj __c_wrapper_mod;"
> "static ScmObj __sandbox_mod;"
> "void Scm_Init_libgauche_hello(void)"
> "{"
> "    SCM_INIT_EXTENSION(libgauche_hello);"
> "    __c_wrapper_mod =
> Scm_ApplyRec0(Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper.c-ffi\",
> FALSE), SCM_SYMBOL(SCM_INTERN(\"ffi-sandbox-module\")), FALSE));"
> "    __sandbox_mod =
> Scm_ApplyRec0(Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper\", FALSE),
> SCM_SYMBOL(SCM_INTERN(\"sandbox-module\")), FALSE));"
> "    internal_init(SCM_MODULE(__c_wrapper_mod));"
> "}"
> "#define Scm_Init_libgauche_hello internal_init"
> "#define INIT_SYMVAL(var, sym) if (!var) { var =
> Scm_GlobalVariableRef(SCM_MODULE(__c_wrapper_mod),
> SCM_SYMBOL(SCM_INTERN(sym)), FALSE); }"
> "static ScmObj cw_boxvar(ScmObj klass, void *varptr)"
> "{"
> "    static ScmObj make_proc = NULL;"
> "    static ScmObj csizeof_proc = NULL;"
> "    ScmObj obj;"
> "    ScmObj buf;"
> "    size_t size;"
> "    INIT_SYMVAL(make_proc, \"make\");"
> "    INIT_SYMVAL(csizeof_proc, \"c-sizeof\");"
> "    size = Scm_GetIntegerU(Scm_ApplyRec1(csizeof_proc, klass));"
> "    buf = Scm_MakeU8VectorFromArrayShared(size, varptr);"
> "    obj = Scm_ApplyRec3(make_proc, klass, SCM_MAKE_KEYWORD(\"buffer\"),
> buf);"
> "    SCM_RETURN(obj);"
> "}"
> "static void cw_unbox(void *dest, ScmObj obj, size_t size)"
> "{"
> "    static ScmObj bufferof_proc = NULL;"
> "    ScmObj buf;"
> "    if (!bufferof_proc) {"
> "        bufferof_proc =
> Scm_GlobalVariableRef(SCM_FIND_MODULE(\"c-wrapper.c-ffi\", FALSE),
> SCM_SYMBOL(SCM_INTERN(\"buffer-of\")), FALSE);"
> "    }"
> "    buf = Scm_ApplyRec1(bufferof_proc, obj);"
> "    memcpy(dest, SCM_UVECTOR_ELEMENTS(buf), size);"
> "}"
> "static ScmObj __c_wrapper_894;    /* add */"
> (define-symbol make-c-func "__c_wrapper_889")
> (define-symbol quote "__c_wrapper_890")
> (define-symbol add "__c_wrapper_891")
> (define-symbol <c-int> "__c_wrapper_892")
> (define-symbol list "__c_wrapper_893")
> (initcode "__c_wrapper_894 = Scm_EvalRec(SCM_LIST4(__c_wrapper_889,
> SCM_LIST2(__c_wrapper_890, __c_wrapper_891), __c_wrapper_892,
> SCM_LIST3(__c_wrapper_893, __c_wrapper_892, __c_wrapper_892)),
> SCM_OBJ(__c_wrapper_mod));")
> (initcode "Scm_Define(SCM_MODULE(__c_wrapper_mod),
> SCM_SYMBOL(__c_wrapper_891), __c_wrapper_894);")
> (initcode "Scm_Define(SCM_MODULE(__sandbox_mod),
> SCM_SYMBOL(__c_wrapper_891),
> Scm_GlobalVariableRef(SCM_MODULE(__c_wrapper_mod),
> SCM_SYMBOL(__c_wrapper_891), 0));")
> ;; Local variables:
> ;; mode: scheme
> ;; end:
> + LD_LIBRARY_PATH=.
> + ./test-libhello.scm
> #?="././test-libhello.scm":13:(add 10 32)
> *** ERROR: function add is not found.
> Stack Trace:
> _______________________________________
>   0  (report-error e)
>   1  (add 10 32)
>         at "././test-libhello.scm":13
>   2  (main args)
>
> greetings
> Jens
>
>
> _______________________________________________
> Gauche-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/gauche-devel
>

_______________________________________________
Gauche-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gauche-devel