Skip to content

Instantly share code, notes, and snippets.

@ncweinhold
Created May 25, 2011 20:44
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ncweinhold/991905 to your computer and use it in GitHub Desktop.
Save ncweinhold/991905 to your computer and use it in GitHub Desktop.
Calling Gambit-C scheme functions from C
#include <stdio.h>
#define ___VERSION 406001
#include "gambit.h"
#include "somescheme.h"
#define SCHEME_LIBRARY_LINKER ____20_somescheme__
___BEGIN_C_LINKAGE
extern ___mod_or_lnk SCHEME_LIBRARY_LINKER (___global_state_struct*);
___END_C_LINKAGE
int main(int argc, char** argv) {
printf("Hello World, this is from C\n");
___setup_params_struct setup_params;
___setup_params_reset (&setup_params);
setup_params.version = ___VERSION;
setup_params.linker = SCHEME_LIBRARY_LINKER;
___setup (&setup_params);
hello_from_scheme();
___cleanup();
return 0;
}
extern void hello_from_scheme(void);
(c-define (hello) () void "hello_from_scheme" "extern"
(write "Hello From Scheme")
(newline))
@sonwh98
Copy link

sonwh98 commented Jul 12, 2021

Hi, how do you compile this? I get the following error. How to prevent the main function from being generated in somescheme_.c ? Thanks!

% 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

@feeley
Copy link

feeley commented Jul 12, 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

@sonwh98
Copy link

sonwh98 commented Jul 18, 2021

#Thanks Marc!

the last line has this problem

sto@big:~/workspace/hello-scm$ gcc somescheme.o somescheme_.o main.o -I$GAMBIT/include $GAMBIT/lib/libgambit.a -lm -ldl -lutil  -lssl -lcrypto                                                                     
ld: error: undefined symbol: ____20_somescheme__                                                                                                                                                                   
>>> referenced by main.c                                                                                                                                                                                           
>>>               main.o:(main)                                                                                                                                                                                    
collect2: error: ld returned 1 exit status   

it seems somescheme_.c

defines ___LNK_somescheme__

line 8 in main.c has to replaced with that symbol instread of __20_somescheme
#define SCHEME_LIBRARY_LINKER _LNK_somescheme

This works now. Thanks again for your help

@feeley
Copy link

feeley commented Jul 18, 2021

Yes this is a "recent" (last year or two) change in the name of the linker function name.

@sonwh98
Copy link

sonwh98 commented Jul 18, 2021

I created more simple examples based on information provided here. Hopefully this will help others who find this gist
https://lambdakids.stigmergy.systems/2021/7/4/gambit-ffi.blog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment