Hello!
This is my first try to use the C interface of gambit-c. I used gambit-c 4.2.8 and mingw gcc compiler from CodeBlocks under Windows XP.
Running alt.exe, I get: Hello World! from C begin 137end
The lines printed from the C function appear before the lines printed by scheme. I have two questions:
1) Why the output lines do not appear in the call order ? 2) Is this the correct way to get small executables with gambit-c or it can be done in a better way ?
Thank you!
functii.c ---------- #include <string.h> #include <stdio.h>
int hello(char * s){ printf("%s",s); return strlen(s); }
test.scm ---------- (c-declare #<<c-declare-end extern int hello(char *s); c-declare-end )
(c-define-type MyInt int) (c-define-type MyString char-string)
(define myHello (c-lambda (MyString) MyInt "hello" ))
alt.scm --------- (display "begin") (newline) (display (myHello "Hello World!\n")) (display (myHello "from C\n")) (display "end") (newline)
compile.cmd ------------ @echo off set PATH=E:\limbaje\CodeBlocks\MinGW\bin;%PATH%
gsc -link -o leg.c test.scm
gcc -shared -mwin32 -m32 -march=pentium4 -IE:\limbaje\gambit-gcc\v4.2.8\include -IE:\limbaje\CodeBlocks\MinGW\include -LE:\limbaje\gambit-gcc\v4.2.8\lib -LE:\limbaje\CodeBlocks\MinGW\lib -o test.dll -Wl,--output-def,test.def -Wl,--kill-at -Wl,--out-implib,test.a -Wl,-s -D___SINGLE_HOST -D___LIBRARY -D___SHARED test.c leg.c functii.c -lgambc -lws2_32
gsc -link -l leg -o lleg.c alt.scm
gcc -mwin32 -m32 -march=pentium4 -IE:\limbaje\gambit-gcc\v4.2.8\include -IE:\limbaje\CodeBlocks\MinGW\include -LE:\limbaje\gambit-gcc\v4.2.8\lib -LE:\limbaje\CodeBlocks\MinGW\lib -D___SINGLE_HOST -D___SHARED -Wl,-s -Wl,--enable-auto-import alt.c lleg.c test.a -o alt.exe
set PATH=%PATH:E:\limbaje\CodeBlocks\MinGW\bin;=%
gcc -v ========
Reading specs from E:/limbaje/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/specs Configured with: ../gcc-3.4.5/configure --with-gcc --with-gnu-ld --with-gnu-as - -host=mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls -- enable-languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shar ed --enable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --ena ble-java-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-sync hronization --enable-libstdcxx-debug Thread model: win32 gcc version 3.4.5 (mingw-vista special)
________ Information from NOD32 ________ This message was checked by NOD32 Antivirus System for Linux Mail Servers. part000.txt - is OK http://www.eset.com
Afficher les réponses par date
I've just tested under RHEL4.6 with gambit-c v4.2.2 and the output is:
begin Hello World! 13From C 7end
On Thu, 19 Jun 2008 12:01:28 +0300, Cristian Baboi cristi@ot.onrc.ro wrote:
Hello!
This is my first try to use the C interface of gambit-c. I used gambit-c 4.2.8 and mingw gcc compiler from CodeBlocks under Windows XP.
Running alt.exe, I get: Hello World! from C begin 137end
The lines printed from the C function appear before the lines printed by scheme. I have two questions:
- Why the output lines do not appear in the call order ?
________ Information from NOD32 ________ This message was checked by NOD32 Antivirus System for Linux Mail Servers. part000.txt - is OK http://www.eset.com
Cristian Baboi wrote:
Hello!
Hi Cristian!
This is my first try to use the C interface of gambit-c. I used gambit-c 4.2.8 and mingw gcc compiler from CodeBlocks under Windows XP.
Running alt.exe, I get: Hello World! from C begin 137end
The lines printed from the C function appear before the lines printed by scheme. I have two questions:
- Why the output lines do not appear in the call order ?
I guess you solved your problem, by looking at your 2nd email.
- Is this the correct way to get small executables with gambit-c or it
can be done in a better way ?
I'm not sure about the size of the executable, but I can give you some tips on compiling scheme programs and writing ffi. ;)
functii.c
#include <string.h> #include <stdio.h>
int hello(char * s){ printf("%s",s); return strlen(s); }
I believe that this file is not required. You could get rid of it and only use scm file instead ^_^.
test.scm
(c-declare #<<c-declare-end extern int hello(char *s);
/* You could either put directly your definition of hello here, but I would also get rid of hello if I was you and do instead: */ #include <stdio.h>
c-declare-end )
(c-define-type MyInt int) (c-define-type MyString char-string)
;;this is not necessary, but I guess you knew it already hehe
(define myHello (c-lambda (MyString) MyInt "hello" ))
;;then here you could have: (define printf (c-lambda (char-string char-string) void "printf")) (define (myHello str) (printf "%s" str) (string-length str))
;; the alt.scm can remain the same
alt.scm
(display "begin") (newline) (display (myHello "Hello World!\n")) (display (myHello "from C\n")) (display "end") (newline)
To compile in linux and run the program I usually do: dave@david /tmp $ gsc -c -o test.c test.scm dave@david /tmp $ gsc -c -o alt.c alt.scm dave@david /tmp $ gsc -link -o alt_.c test.c alt.c dave@david /tmp $ gcc -I/opt/gambit-c/current/include -o alt.exe test.c alt.c alt_.c -L/opt/gambit-c/current/lib -lgambc -lutil -lm -ldl dave@david /tmp $ ./alt.exe begin Hello World! 13from C 7end
You would just need to change to compile flags to match thoses required by windows (-lws2_32, etc...). Again, I don't know if this results in the smallest executable but this is the way I compile my scheme program into executables (I usually compile the .c to .o seperately, but this does not change much).
I hope I could help you a bit!
David
PS: I attached to source code file I compiled
Cristian Baboi wrote:
This is my first try to use the C interface of gambit-c. I used gambit-c 4.2.8 and mingw gcc compiler from CodeBlocks under Windows XP.
Running alt.exe, I get: Hello World! from C begin 137end
The lines printed from the C function appear before the lines printed by scheme. I have two questions:
- Why the output lines do not appear in the call order ?
I guess you solved your problem, by looking at your 2nd email.
I solved it by moving to an other O.S. :-) I've not solved it in Windows XP.
- Is this the correct way to get small executables with gambit-c or it
can be done in a better way ?
I'm not sure about the size of the executable, but I can give you some tips on compiling scheme programs and writing ffi. ;)
Thank you. The sizes I get are: 6.50 KB for the executable and 2.98 MB for the library.
I compiled your modified files on Windows and I still get: Hello World! from C begin 137end
I compiled test.scm in libtest.dll (2.98 MB - includes all gambitc library) and alt.scm in alt.exe (6.50 KB) I have not tryed to make only a single executable file as you did.
You would just need to change to compile flags to match thoses required by windows (-lws2_32, etc...). Again, I don't know if this results in the smallest executable but this is the way I compile my scheme program into executables (I usually compile the .c to .o seperately, but this does not change much).
On Linux I've got libtest.so - 6.4 KB, alt.exe - 6.6 KB striped and libgambc.so - 4.2 MB unstriped
I hope I could help you a bit!
Thank you!
________ Information from NOD32 ________ This message was checked by NOD32 Antivirus System for Linux Mail Servers. part000.txt - is OK http://www.eset.com
On Thu, 19 Jun 2008 15:42:59 +0300, David St-Hilaire sthilaid@iro.umontreal.ca wrote:
Cristian Baboi wrote: To compile in linux and run the program I usually do: dave@david /tmp $ gsc -c -o test.c test.scm dave@david /tmp $ gsc -c -o alt.c alt.scm dave@david /tmp $ gsc -link -o alt_.c test.c alt.c dave@david /tmp $ gcc -I/opt/gambit-c/current/include -o alt.exe test.c alt.c alt_.c -L/opt/gambit-c/current/lib -lgambc -lutil -lm -ldl dave@david /tmp $ ./alt.exe begin Hello World! 13from C 7end
This way I've got 7.6 KB on linux (+ gambit library ) and 2.39 MB on Windows XP (one file wich includes the gambit library)
On Windows, with or without DLL, with or without -D___SINGLE_HOST, I still get: Hello World! from C begin 137end
________ Information from NOD32 ________ This message was checked by NOD32 Antivirus System for Linux Mail Servers. part000.txt - is OK http://www.eset.com
Cristian Baboi wrote:
- Why the output lines do not appear in the call order ?
Gambit does it's own buffering (it doesn't use the C stdio functions, iirc). So you need to make sure output is flushed in both worlds when you want it (check |force-output| in the Gambit manual, and the fflush manpage for stdio).
Christian.
On Thu, 19 Jun 2008 17:45:44 +0300, Christian Jaeger wrote:
Cristian Baboi wrote:
- Why the output lines do not appear in the call order ?
Gambit does it's own buffering (it doesn't use the C stdio functions, iirc). So you need to make sure output is flushed in both worlds when you want it (check |force-output| in the Gambit manual, and the fflush manpage for stdio).
Thank you. It works now.
________ Information from NOD32 ________ This message was checked by NOD32 Antivirus System for Linux Mail Servers. part000.txt - is OK http://www.eset.com
On 20-Jun-08, at 5:22 AM, Cristian Baboi wrote:
On Thu, 19 Jun 2008 17:45:44 +0300, Christian Jaeger wrote:
Cristian Baboi wrote:
- Why the output lines do not appear in the call order ?
Gambit does it's own buffering (it doesn't use the C stdio functions, iirc). So you need to make sure output is flushed in both worlds when you want it (check |force-output| in the Gambit manual, and the fflush manpage for stdio).
Thank you. It works now.
I'll see if I can't figure out a way to have the same stdio buffering on Windows as the other platforms. That will avoid such confusion.
Marc