Calling Rust from C (project)
In this section, we will demonstrate the setup needed to build a Rust shared library (with a .so
extension on Linux) incorporating an FFI interface and invoke it from a C program. The C program would be a simple program that just prints out a greeting message. The example is deliberately kept simple to enable you (as you're not expected to be familiar with complex C syntax) to focus on the steps involved, and for easy verification of this first FFI program in a variety of operating system environments.
Here are the steps that we will go through to develop and test a working example of a C program that calls a function from a Rust library using the FFI interface:
- Create a new Cargo lib project.
- Modify
Cargo.toml
to specify that we want a shared library to be built. - Write an FFI in Rust (in the form of a C-compatible API).
- Build the Rust shared library.
- Verify whether the Rust shared library has been built correctly.
- Create...