23/12/2009 C wrapping of C++ code

At work I have to integrate my C++ code in a large C code ... an opportunity for my to test for the first time an experience of writing a C wrapper of my C++ code ... which turns out to be really easy!

Everything is based on extern "C" qualifier, #ifdef __cplusplus and a proper API design. Actually, I have been really suprized how versatile the code can become between C and C++. Use either one of the other, with a good and simple design, that's fine.

header.h
  • #ifndef __cplusplus
  • extern "C"
  • #endif //__cplusplus
  • void func();
source.cpp
  • #include "header.h"
  • #include <iostream>
  • extern "C" void func()
  • {
  • std::cout << "This is C++ code!" << std::endl;
  • }
source.c
  • #include "header.h"
  • int main()
  • {
  • func();
  • }

On the design area, I like to create C structures that contain a void pointeur to store the C++ object. It brings a higher typed and object oriented approached to C which fit well with C++ code. For each operation, I call the C function with the right 'object' to modify It follows the "direct state access" way of OpenGL if you want as opposed to the "bind and edit" classic way of OpenGL.

The C++ Experiments set of tests has been updated with this C wrapper experiment.

GLEW 1.5.2 released with OpenGL 3.2 support >
< OpenGL 3 Samples Pack 1.2.1 released
Copyright © Christophe Riccio 2002-2016 all rights reserved
Designed for Chrome 9, Firefox 4, Opera 11 and Safari 5