[Openmcl-devel] CCL <-> C++ interface generator in alpha, reviewers wanted

Jason E. Aten j.e.aten at gmail.com
Mon Apr 4 14:51:35 PDT 2011

If anyone is interested in C++ interfaces from CCL, please read on.

I've finished modernizing (a nice word for hacking the heck out of) the CFFI
module SWIG to wrap my large-ish C++ codebase so that I can call it and test
it from CCL. I can't submit my changes back to SWIG without writing a bunch
of runtime tests for William the maintainer; those tests  have yet to be
written.  And I'd rather eat my own dogfood for a while to iron out any
latent bugs.  Nonetheless, everything compiles and the resulting plugins
load cleanly without manual code changes, which was my goal. So while it
isn't publicly available, I would like to have it reviewed for obvious
blunders (quite likely since I'm new to CL).

That is, I'd appreciate having a more experienced Lisper's perspective on
the design while the metal is still hot and maleable.

If you are interested in trying the new cffi.cxx or reviewing the
auto-generated Lisp interface, please contact me off list.  Even cursory
feedback would be appreciated.  I'll paste a micro example below.

Thank you.


A micro example of calling into arity-overloaded C++ methods from lisp:

;;;;;;;;;;;;;;;; file:  micro_test.cl  (adapt the path in the :cd command as
appropriate)  ;;;;;;;;;;;;;;;;;;;;;

(eval-when (:execute :load-toplevel :compile-toplevel)
(require :cffi))

(cl:defpackage :micro
  (:shadowing-import-from :cffi :defcallback)
  (:use :cl :ccl :cffi))
(in-package :micro)

(:cd "/home/jaten/dj/micro")
(ccl:open-shared-library "/home/jaten/dj/micro/micro.so")

(load (compile-file "micro.lisp")); :print nil :verbose nil)
(load (compile-file "micro-clos.lisp"))

(setf m (make-instance 'MicroTestCppClass))

;; Demonstrate calls to methods that are overloaded with different arity in
;; Since Common Lisp doesn't support method overloading (apparently,
;; please correct me if I've misunderstood the comments at the bottom of
;; page 194 of Practical Common Lisp), I had to give
;; each of these guys a differnt name/symbol...
(method_MicroTestCppClass_cpp_overloaded_method0 m)
;;; output:
; 8
(method_MicroTestCppClass_cpp_overloaded_method1 m 10)
;;; output:
; In MicroTestCppClass::cpp_overloaded_method : returning your argument plus
; 19

;;;;;;;;;;;;;;;; file:  micro.h   ;;;;;;;;;;;;;;;;;;;;;

#ifndef MICRO_H
#define  MICRO_H

#include <stdio.h>

class MicroTestCppClass {

  int cpp_overloaded_method();
  int cpp_overloaded_method(int an_int_argument);


#endif // MICRO_H

;;;;;;;;;;;;;;;; file:  micro.i   ;;;;;;;;;;;;;;;;;;;;;

%module micro

 #include "micro.h"


%include "micro.h"

;;;;;;;;;;;;;;;; file:  micro.cpp   ;;;;;;;;;;;;;;;;;;;;;

#include <stdio.h>
#include "micro.h"

int MicroTestCppClass::cpp_overloaded_method() {
  printf("\n In MicroTestCppClass::cpp_overloaded_method : returning 8\n");
  return 8;

int MicroTestCppClass::cpp_overloaded_method(int an_int_argument) {
  printf("\n In MicroTestCppClass::cpp_overloaded_method : returning your
argument plus 9\n");
  return an_int_argument + 9;

;;;;;;;;;;;;;;;; file:  Makefile   ;;;;;;;;;;;;;;;;;;;;;

all: micro

    swig -cffi -c++  micro.i
    g++ -c micro_wrap.cxx -fpic -o micro_wrap.o
    g++ -c micro.cpp      -fpic -o micro.o
    g++ -shared micro.o micro_wrap.o -o micro.so

    ccl < micro_test.cl

    rm -f *.o micro_wrap.cxx micro-clos.lisp micro.lisp micro.so *.lx64fsl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20110404/53cf4620/attachment.htm>

More information about the Openmcl-devel mailing list