Google helps developers bind C++ to Python

Google's unofficial new project uses LLVM to generate language wrappers for C++ libraries and has Python as the first of many possible targets

Google helps developers bind C++ to Python
David J. Fred (CC BY-SA 2.5)

Fans of C++ praise its power and speed, but even they admit its complexity can be daunting. That's true when you're writing libraries in the language, and it's doubly true when you want to make C++-authored libraries useful to other languages. Writing wrappers for C++ libraries is tough work.

CLIF, an open source project developed at Google (but not an official Google product), is a framework for automatically generating C++ library bindings for multiple languages. It's written in Python, and so far the only included language binding is for Python, both versions 2 and 3, but the plan is to allow other language parsers to be written for it.

CLIF uses the LLVM compiler framework and its Clang C/C++ front end to parse out the type information from a C++ API description. The resulting information is used to generate a wrapper, and any type conversions that need to be performed to or from the target language are done with a runtime C++ library.

Aside from LLVM/Clang, CLIF uses a few other elements, but nothing outlandish—Google's protobuf ("for inter-process communication between the CLIF front end and back end"), and the CMake build system. Interestingly, it does not use Google's custom build system, Bazel, but that may be to reduce dependencies not found in common C++ toolchains, as CMake is far more common than Bazel.

Long-standing projects already exist for generating language wrappers for C/C++ libraries. SWIG, among the best known and most commonly used, is able to generate wrappers for a broad variety of languages. But SWIG is also a legacy project—its original version debuted in 1995—and it has long been regarded, even by its creator, core Python developer David Beazley, as having flaky support for C++. "Almost all of these bugs [for C++ support in SWIG] were due to the fact that SWIG started out as a hack and didn't really have any kind of solid conceptual foundation for how it should be put together," wrote Beazley.

In CLIF's case, the project tries to avoid some of the problems with parsing C++ by leaving that job to LLVM, rather than trying to reinvent the wheel. Some of the other issues that arise with C++, like how to handle C++ templates via a wrapper, are dealt with in CLIF by way of its C++ runtime. Some advanced C++ features, like inheritance, aren't exposed by default, but can be if needed.

One feature that doesn't appear to be available yet is mapping exceptions in C++ to counterparts in other languages, especially Python. It's certainly worth adding later on after some more work has been done.

Despite its age and encroaching competition from languages like Rust, C++ remains a language of choice for high-performance computing, including AI applications. And the level of tooling for the language is robust: The GCC compiler recently added experimental support for features in the C++17 standard.

Copyright © 2017 IDG Communications, Inc.