Cpp2C: An extern C wrapper code generator for C++

What is cpp2c?

Cpp2C is an (extern) C wrapper code-generator for C++ code.

Cpp2C gets a C++ header file path and generate a thin C++ wrapper code with extern-C interface to that header file, allowing you to either statically link it to a C code or create a dynamic library with the proper extern-C interface and def file being auto-generated.

Why do I need it?

Possible uses:
  • Create an extern C interface to a C++ code, to be used as a dynamic library (including .def file generation).
  •  Reuse C++ code in a C program, either to improve performance or due-to partial support of the platform of C++ features (embedded\real-time operating systems come to mind).

 

Features

  • Transforms classes to opaque pointers, and (public) methods\operators\constructors\destructors to functions.
  •  Masks exceptions and returns them via output variable.
  • Inheritance and Polymorhism are implicitly supported by the C++ compiler.
  •  Handles function\operator overloading by creating a unique function name for each overloaded version.
  • Generates code for template instantiations (i.e. you cannot export the code of vector - but you can export the code of vector<int>), though the enforcement of the template instantiation (e.g. via explicit declaration in the header file:  template class std::vector<int>, etc.) is at the user's responsibility.
  • Supports reference variables as pointers.
  • Supports static methods.
  • Supports boolean variables for non-C99 supported compilers.

How to run cpp2c?

After downloading Cpp2C and all the required dependencies, stated here, you can simple run, for example:

cpp2c.py <c++_header_file_to_wrap_path>

under Linux, using gcc, or:

cpp2c.py <c++_header_file_to_wrap_path> -g <gcc_xml_path> -t msvc9

under Windows, using Visual Studio 2008.

 

You can view all the script parameters by typing:

cpp2c.py -h

Usage Examples

Examples for common cases:

Recent Forum Posts

No recent posts

Recent Blog Entries

No recent entries