Calling functions from <linux/*> libs from rust

I am new to using rust FFI. My problem wants me to use <linux/*.h> libraries. For example, <linux/if.h> and <linux/if_tun.h>. How do I use certain functions or constants defined in these libs? Are there any resources for calling c functions from rust? Are there any example code that does the same?

The nomicon has a chapter on ffi here, and you'll probably like the crate bindgen which can convert header files to rust bindings.

1 Like

The nix crate may have some Rust wrappers for such functions. You can also use the cc crate to compile your own C library that exposes these to Rust in a nice way. That's often needed if the C headers rely on non-trivial macros.

1 Like

Thanks. I have been using cc crate for my purpose since yesterday. I have encountered a new problem. I have to set capabilities on my binary. Is there any way to do it with the cargo build process. I googled it but I didn't find anything usable.

There's nothing built-in to cargo, but there are ways to solve this problem. One way is to use an external tool, such as cargo-make. I'm personally a fan of the cargo-xtask scheme, since it doesn't require installation of any additional tools.

1 Like

It's a missing feature

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.