If you're writing a C binding, you might want to have a tool that automatically parses C and C ++ header files. c2ffi is a tool that uses LLVM, and is a convenient tool that analyzes C and C ++ header files and creates a json file of metadata.
This article records the steps taken to use c2ffi on Ubuntu.
git clone https://github.com/rpav/c2ffi
Switch branches according to the LLVM version. Here, the LLVM of the package distributed by Ubuntu is version 10, so switch to 10.
sudo apt install llvm-10-dev libclang-cpp10-dev
git branch -a #View branch list
git checkout -b llvm-10.0.0 refs/remotes/origin/llvm-10.0.0
Now, as the official README says, when I create a build
directory and run cmake ..
, it says cmake is out of date.
mkdir build
cd build
cmake ..
An error occurs
CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
CMake 3.17 or higher is required. You are running version 3.16.3
-- Configuring incomplete, errors occurred!
Therefore, there is no choice but to install the latest version of cmake.
cd ../../ #Outside the c2ffi directory
git clone https://github.com/Kitware/CMake
cd CMake
git checkout -b v3.19.2 refs/tags/v3.19.2 #Latest release version
./bootstrap && make # -j8
Wait a few minutes.
CMake is probably used in a variety of tools, so there's a good chance you'll get into trouble later. Do checkinstall
instead of make install
so that you can easily uninstall it later.
sudo checkinstall
Modify the options as appropriate and install. Since cmake is large, this process also takes some time. Once you're done, restart the shell and you'll be able to use cmake. Check if cmake can be used.
#Rebooting the shell
cmake --version
# cmake version 3.19.2
The latest version of CMake has been installed. You will be returned to the c2ffi directory.
cd ../c2ffi
I wonder if it will work this time
cd build
cmake ..
make # -j8
I get the error message / usr/bin/ld: -lclang-cpp not found
. This does not disappear even if you do sudo apt install libclang-cpp10-dev
etc. Refer to the following issue
LIBRARY_PATH=/usr/lib/llvm-10/lib/ make
will do. I went to the end with this. After reading the article in this issue, it seems that you may need another environment variable on your Mac as well. checkinstall. I think that Summary and Name will not be specified correctly if it is left as it is, so rewrite it to c2ffi.
sudo checkinstall
This will end soon. Type c2ffi
to see if you can use c2ffi correctly.
c2ffi: unrecognized option '--version'
Usage: c2ffi [options ...] FILE
Options:
-I, --include Add a "LOCAL" include path
-i, --sys-include Add a <system> include path
-D, --driver Specify an output driver (default: json)
-o, --output Specify an output file (default: stdout)
-M, --macro-file Specify a file for macro definition output
--with-macro-defs Also include #defines for macro definitions
-N, --namespace Specify target namespace/package/etc
-A, --arch Specify the target triple for LLVM
(default: x86_64-pc-linux-gnu)
-x, --lang Specify language (c, c++, objc, objc++)
--std Specify the standard (c99, c++0x, c++11, ...)
-E Preprocessed output only, a la clang -E
I think it's OK if it is displayed like this.
After that, let's use c2ffi to accompany the binding creation.
Officially, we provide common-lisp and a bindig generation tool for Ruby, but Ruby is older so I'm not sure if it still works.
That's all for this article.
Recommended Posts