12

I'm in the process of writing a C interface to a C++ library, and I'm looking for some high-quality examples (or best practices).

So far this one seems pretty promising: http://czmq.zeromq.org/manual:czmq

Any other suggestions?

jjkparker
  • 27,597
  • 6
  • 28
  • 29
  • I was just going to recommend ZeroMQ, glad I read the question through :) – Fred Foo Nov 04 '11 at 11:42
  • Possible duplicate: http://stackoverflow.com/q/7953559/440558 – Some programmer dude Nov 04 '11 at 11:43
  • Definitely not a duplicate of that question. This question asks for examples of how to do it well (that is, how to design the C API based on a C++ API). That other question is solely interested in the basic mechanics of doing it at all. – Steve Jessop Nov 04 '11 at 11:49

6 Answers6

1

Another high quality example is the Open Dynamics Engine. It has a C++ backend and a C frontend. Everything is C linkable.

w177us
  • 332
  • 5
  • 13
1

You could look into the Parma Polyhedra Library as an example of excellent C interface to a well written C++ library. PPL is a free GPL-ed software, notably used inside the GCC compiler.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

libzmq is a kind of weird case since the low-level C API was originally meant to look like POSIX sockets, and absolutely not object-oriented (we made it more consistent and organized over time). Meanwhile the actual library is in C++.

The C++-to-C interface is in libzmq/src/zmq.cpp, and consists of a bunch of simple C functions that call the 'real' C++ code.

CZMQ on the other hand aims for something more classy, providing a simple class model with constructors, destructors, containers, private properties, etc. Nothing radical but does turn C into a much more elegant language.

I'm not sure how well the CZMQ class approach would map to a C++ API unless that API was explicitly designed to be mapped.

Disclaimer: I'm the author of most of CZMQ.

Pieter Hintjens
  • 6,599
  • 1
  • 24
  • 29
0

If your C++ library is written as COM on Windows. There are tools to automatically generate the C interface for it.

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
0

I can suggest FTGL which is a C++ library providing a C interface. Here are two sample programs that achieve exactly the same thing:

Note also that FTGL uses the pImpl paradigm in order to achieve binary compatibility across versions. See here why it's good.

Disclaimer: I'm an FTGL contributor.

sam hocevar
  • 11,853
  • 5
  • 49
  • 68
0

libGLU (OpenGL Utility Library) is partially written in C++: http://cgit.freedesktop.org/mesa/mesa/tree/src/glu

hmn
  • 716
  • 7
  • 18