Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Bindgen tools in Rust FFI programming

Shulou Source: shulou.com Published: 2022-06-01 06:54:28 10月05日 Update

This article is about how to use the Bindgen tool in Rust FFI programming. I think it is very practical, so I share it with you. I hope you can get something after reading this article.

Bindgen introduced us to introduce the rust-bindgen project. The project address is: https://github.com/rust-lang/rust-bindgen. As you can see, this is an official incubation project of Rust, which is quite mature at present. This article is only a general introduction, not a complete explanation of the bindgen project, nor a comprehensive translation of bindgen tutorial. Therefore, when you really do it, you need to carefully read the original Tutorial text and consult the relevant API. Bindgen is an auxiliary library and command-line tool that automatically generates Rust bindings for C (or C++) libraries. C++ 's (currently) support is incomplete. In other words, bindgen can be used as a crate, in conjunction with Cargo's build.rs mechanism, to automatically generate Rust binding files during the build phase before compiling the body Rust code according to the header file (.h, .hpp) of Cmax Candle +. Specifically, check out the example: https://rust-lang.github.io/rust-bindgen/tutorial-0.htmlbindgen has another use, which itself provides a command-line tool. Execute: cargo install bindgen

It can be installed. Then, at the command line, use the

Bindgen input.h-o bindings.rs

The Rust binding file bindings.rs can be generated dynamically according to the C header file input.h. It's simple. But the real scene is not as pure as imagined. So there is a lot of possible repair work. Bindgen provides us with various ways to fix it (this is the essence). Specifically, there are the following measures: whitelist blacklist Opaque object type replacement some of these measures can be used in library mode, command line mode, and in the source code (C) comments to add annotations in the mode. Some can cover all three, and some can only cover one or two of them. A detailed explanation can be found on the https://rust-lang.github.io/rust-bindgen/customizing-generated-bindings.html page. At the same time, bindgen also has corresponding strategies to deal with the characteristics of Union and Bitfields of C. These may often be encountered in the actual packaging process. Bindgen itself is not very knowledgeable. The above is how to use the Bindgen tools in Rust FFI programming. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Tools commands files projects patterns knowledge generation programming measures more knowledge points articles caching patching work maturity subject that is the original text Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi macOS Microsoft Shulou Technology NVidia