English / Japanese

c-wrapper - A Generic Wrapper for C Libraries

Last modified: 9 Aug 2009

What's c-wrapper?

c-wrapper is a foreign function interface for C and Objective-C libraries. It can parse C header files, so you don't need to define functions, global variables and constants to use libraries.

Sample code

To use c-wrapper, you can write a code like below.

(use c-wrapper)

(c-load "stdio.h")

(printf "Hello, world\n")

In MacOSX, you can use Objective-C libraries.

(use objc-wrapper)

(c-load "Cocoa/Cocoa.h"
        :libs "-framework Foundation -framework Cocoa")

[[NSAutoreleasePool :alloc] :init]

(define s [[NSSpeechSynthesizer :alloc] :init])

[s :startSpeakingString (@ "Hello, world")]

(do () ((eq? [s :isSpeaking] NO)))


c-wrapper requires Gauche 0.8.14 or later and GCC. I have checked the environments below.



To install, use gauche-package or type configure & make manually.


Simply type a below command.

% gauche-package install c-wrapper-0.6.1.tgz

configure & make

Type below commands. make must be GNU make.

% tar zxvf c-wrapper-0.6.1.tgz
% cd c-wrapper-0.6.1
% ./configure
% make
% make install

How to use cwcompile

cwcompile is a tool which parses header files and generates a shared library. This makes loading time shorter because parsing headers is not needed.

For example, if you'd like to generate ImageMagick's MagickWand API module, write a code using c-wrapper, first.

(define-module mymodule
  (use c-wrapper)
  (c-load '("stdio.h" "wand/magick_wand.h")
          :cppflags-cmd "Wand-config --cppflags"
          :ldflags-cmd "Wand-config --ldflags"
          :libs-cmd "Wand-config --libs"
          :compiled-lib "magicklib")
(provide "mymodule")

Next, run the following command.

% cwcompile mymodule.scm

Put magicklib.so, which is generated by cwcompile, on dynamic load paths. And mymodule.scm will load it instead of parsing header files.

Here is a sample movie of c-wrapper and cwcompile.



2009-8-9: release version 0.6.1
2009-6-27: release version 0.6.0
2009-2-21: release version 0.5.6
2009-2-14: release version 0.5.5
2008-5-11: release version 0.5.4
2007-5-21: release version 0.5.2
2007-1-6: release version 0.5.1
2006-12-26: release version 0.5.0
2006-08-25: release version 0.4.4
2006-08-20: release version 0.4.3
2006-08-06: release version 0.4.2
2006-07-24: release version 0.4.1
2006-06-11: release version 0.4.0
2006-04-30: release version 0.3.4
2006-04-15: release version 0.3.3
2006-04-02: release version 0.3.2
2006-03-25: release version 0.3.1
2006-03-19: release version 0.3.0
2006-03-12: release version 0.2.0
2006-03-04: release version 0.1

naoki at koguro dot net