DEV Community

kojix2
kojix2

Posted on

Installing c2ffi on Ubuntu

What is c2ffi?

c2ffi is a tool that converts C and C++ header files to JSON. It is mainly used for creating bindings using ffi.

GitHub logo rpav / c2ffi

Clang-based FFI wrapper generator

Version Notice

You need to use the correct branch of c2ffi for your version of LLVM/Clang:

  • Anything earlier: unsupported
  • 11.0.0: branch llvm-11.0.0 deprecated
  • 12.0.0: branch llvm-12.0.0 old
  • 13.0.0: branch llvm-13.0.0 current
  • 14.0.0: branch llvm-14.0.0 current finally!

Developement will always take place in llvm-X.Y, according to the appropriate version of LLVM. The master branch has been removed. Check out the appropriate version for your LLVM.

c2ffi now provides tagged versions as well. To hopefully avoid confusion these correlate to the appropriate version of LLVM, along with an additional release number for c2ffi, e.g.:

  • v11.1.0.0: For LLVM 11.1.0, c2ffi release 0
  • v11.1.0.1: For LLVM 11.1.0, c2ffi release 1

c2ffi

This is a tool for extracting definitions from C, C++, and Objective C headers for use with foreign function call interfaces. For instance:

#define FOO (1 << 2)
const int BAR = FOO + 10;

typedef
Enter fullscreen mode Exit fullscreen mode

Environment

  • Ubuntu: groovy
  • llvm-11

Install dependencies

sudo apt install llvm-dev libclang-dev libclang-cpp10-dev
sudo apt install checkinstall
Enter fullscreen mode Exit fullscreen mode

Cmake 3.17 or above

The version of cmake distributed by APT is 3.16, and it does not work with c2ffi.

git clone https://github.com/Kitware/CMake
cd CMake
git checkout -b v3.19.2 refs/tags/v3.19.2 # latest version
./bootstrap && make # -j8
sudo checkinstall
Enter fullscreen mode Exit fullscreen mode

Check cmake version.

cmake --version
Enter fullscreen mode Exit fullscreen mode

Build c2ffi

git clone https://github.com/rpav/c2ffi
git checkout -b llvm-11.0.0 refs/remotes/origin/llvm-11.0.0

cd build
cmake .. # -DBUILD_CONFIG=Release
LIBRARY_PATH=/usr/lib/llvm-11/lib/ make # -j8
sudo checkinstall
Enter fullscreen mode Exit fullscreen mode

Check if c2ffi work.

c2ffi
Enter fullscreen mode Exit fullscreen mode

This article is over.
Have a nice day!

Top comments (0)