-
-
Save ncweinhold/991905 to your computer and use it in GitHub Desktop.
#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)) |
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
#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
Yes this is a "recent" (last year or two) change in the name of the linker function name.
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
Hi, how do you compile this? I get the following error. How to prevent the main function from being generated in somescheme_.c ? Thanks!