[gambit-list] Mixing C++ with Scheme

Marc Feeley feeley at iro.umontreal.ca
Sun Mar 4 11:16:02 EST 2012


On 2012-03-02, at 2:33 PM, Patrick Bene wrote:

> Hello,
> 
> I'm trying to mix Gambit Scheme with C++ code and want to know the best way of
> going about it. My goal is to compile into an executable where the C++ code 
> contains the main function, and also exports some functions for the Scheme
> code to use.
> 
> What is the best way to do this?
> 
> Thanks.

There's an example in the Gambit distribution (the file misc/simple-make-project.tgz) which shows how to  mix Gambit Scheme compiled code with a C "main" function.  I also provides a generic "makefile", easily modified for your particular needs (I am copying it below).  If you are interested in mixing with C++, the same example will work, just make sure you use the --enable-cplusplus when you configure Gambit.

Marc


# Simple makefile for project built with Gambit-C system.

# Copyright (c) 2010 by Marc Feeley, All Rights Reserved.

# This makefile is designed to depend only on the gsc compiler.  The
# gsc compiler will invoke the appropriate C compiler and linker, even
# for compiling the C source files of the project.  This makes this
# makefile very portable as many system dependencies have been taken
# care of when Gambit was installed.  If gsc isn't installed in a
# standard location, define GSC accordingly here:

GSC = gsc

# You should redefine C_MODULES and SCHEME_MODULES as needed for your
# project.  Define ALL_MODULES_O as the set of object files for the C
# and Scheme modules.  Define PROG_NAME as the program name.

PROG_NAME = prog
C_MODULES = mod1.c mod2.c main.c
SCHEME_MODULES = mod3.scm mod4.scm
SCHEME_MODULES_C = mod3.c mod4.c
ALL_MODULES_O = mod1.o mod2.o main.o mod3.o mod4.o

# You may want to add stuff to CC_OPTIONS and LD_OPTIONS (directories
# for header files, libraries, etc).  You must keep -D___LIBRARY in
# CC_OPTIONS so that the compiled Scheme code does not implement its
# own "main" function (in the link file generated by gsc).

CC_OPTIONS = -D___LIBRARY
LD_OPTIONS = 

# The rest of the makefile shouldn't need to be modified.

GSC_OPTIONS = -cc-options "$(CC_OPTIONS)" -ld-options "$(LD_OPTIONS)"
#GSC_OPTIONS = -cc-options "$(CC_OPTIONS)" -ld-options "$(LD_OPTIONS)" -verbose
ALL_MODULES_C = $(C_MODULES) $(SCHEME_MODULES_C)
ALL_MODULES = $(C_MODULES) $(SCHEME_MODULES)

.SUFFIXES:
.SUFFIXES: .scm .c .o

all: $(PROG_NAME)

$(PROG_NAME): $(ALL_MODULES_O) $(PROG_NAME)_.o
	$(GSC) $(GSC_OPTIONS) -exe -o $@ $^

$(PROG_NAME)_.c:  $(SCHEME_MODULES_C)
	$(GSC) -link -o $@ $^

.c.o:
	$(GSC) $(GSC_OPTIONS) -obj $^

.scm.c:
	$(GSC) $(GSC_OPTIONS) -c $^

run: $(PROG_NAME)
	./$(PROG_NAME)

clean:
	rm -f $(SCHEME_MODULES_C) $(ALL_MODULES_O) $(PROG_NAME)_.* $(PROG_NAME)


# Note: It is possible to compile and link a program in a single
# invocation of gsc but this is not recommended for large projects
# which usually benefit from separate compilation.  The command is:
#
#   $(GSC) $(GSC_OPTIONS) -exe -o $(PROG_NAME) $(ALL_MODULES)
#
# Moreover, this requires changing the name of the link file in main.c
# from prog_.c to mod4_.c (the name is derived from the last Scheme
# source file in ALL_MODULES).




More information about the Gambit-list mailing list