Calling C++ (not C) from Common Lisp?

c, c++, common-lisp

Solution

Oh, wait!

It seems that there is a trick I can use!

I write a wrapper in C++, declaring wrapper functions extern "C":

#include "lib.h"

extern "C" int lib_operate (int i, double *x) {
...
}

The header file lib.h, which can be called from both C and C++, is:

#if __cplusplus
extern "C" {
#endif

int lib_operate (int i, double *x);

#if __cplusplus
}
#endif

Then compile with:

g++ -c lib.cpp
gcc -c prog.c
gcc lib.o prog.o -lstdc++ -o prog

Seems to work for a toy example! :-)

So, in Common Lisp I'd call the wrapper after loading libstdc++.

Anyway, thank you for your answers!

Problem

I am wondering if there is some way to call C++ code from Common Lisp (preferably portably, and if not, preferably in SBCL, and if not, well, then Clozure, CLisp or ECL). The C++ would be called inside loops for numeric computation, so it would be nice if calls were fast. CFFI seems to not support this: "The concept can be generalized to other languages; at the time of writing, only CFFI's C support is fairly complete, but C++ support is being worked on." (chapter 4 of the manual) SBCL's manual doesn't mention C++ either; it actually says This chapter describes SBCL's interface to C programs and libraries (and, since C interfaces are a sort of lingua franca of the Unix world, to other programs and libraries in general.) The C++ code uses OO and operator overloading, so it really needs to be compiled with g++. And as far as I know, I can have a C++ main() function and write wrappers for C functions, but not the other way around -- is that true? Anyway... Is there some way to do this? Thank you!

Original source