[gambit-list] printf and the C interface

David St-Hilaire sthilaid at iro.umontreal.ca
Thu Jun 19 08:42:59 EDT 2008

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:
> 1) Why the output lines do not appear in the call order ?

I guess you solved your problem, by looking at your 2nd email.

> 2) 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 at david /tmp $ gsc -c -o test.c test.scm
dave at david /tmp $ gsc -c -o alt.c alt.scm
dave at david /tmp $ gsc -link -o alt_.c test.c alt.c
dave at 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 at david /tmp $ ./alt.exe
Hello World!
13from C

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!


PS: I attached to source code file I compiled
-------------- next part --------------
A non-text attachment was scrubbed...
Name: alt.scm
Type: text/x-scheme
Size: 122 bytes
Desc: not available
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20080619/71593de5/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.scm
Type: text/x-scheme
Size: 263 bytes
Desc: not available
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20080619/71593de5/attachment-0001.bin>

More information about the Gambit-list mailing list