[gambit-list] Calling Gambit Scheme functions from C

Marc Feeley feeley at iro.umontreal.ca
Mon Jul 12 17:37:34 EDT 2021

By default the link file (here somescheme_.c generated from somescheme.c) contains a C “main” function so that without any additional step the code can be compiled and linked (at the C level) to obtain an executable program. In other words the entry point of the program will be the Gambit runtime system.

However your file main.c also defines a C “main” function so you end up with a duplicate definition.  The way around this, shown below, is to compile the link file with the -D___LIBRARY C compiler option.  This will cause the somescheme_.c file to avoid the inclusion of a C “main” function (through a series of #ifdefs).  In other words, the Scheme code and the Gambit runtime system are acting as a library to the main C program.


gsc -c somescheme.scm
gsc -link somescheme.c
gsc -obj -cc-options -D___LIBRARY somescheme.c somescheme_.c          <========= need the -cc-options here
gsc -obj main.c
gcc somescheme.o somescheme_.o main.o -I$GAMBIT/include $GAMBIT/lib/libgambit.a -lm -ldl -lutil  -lssl -lcrypto

> On Jul 12, 2021, at 3:57 PM, Sonny To <son.c.to at gmail.com> wrote:
> Hi,
> Found an example of calling Scheme functions from C but cannot compile it
> https://gist.github.com/ncweinhold/991905
> % gsc -c somescheme.scm
> % gsc -link somescheme.c
> % gsc -obj somescheme.c main.c somescheme_.c
> % gcc somescheme.o somescheme_.o main.o -I$GAMBIT/include
> $GAMBIT/lib/libgambit.a -lm -ldl -lutil  -lssl -lcrypto
> ld: error: duplicate symbol: main
>>>> defined at somescheme_.c
>>>>           somescheme_.o:(main)
>>>> defined at main.c
>>>>           main.o:(.text+0x0)
> collect2: error: ld returned 1 exit status
> How to prevent the main function generation in somescheme_.c  ?
> Many thanks for any help.
> -Sonny
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://mailman.iro.umontreal.ca/cgi-bin/mailman/listinfo/gambit-list

More information about the Gambit-list mailing list