Calling C code in Rust requires the following steps:
- Write or obtain code in C language.
- Creates Rust's external function interface (FFI).
- Using Rust
unsafe
Block calls C function.
Let’s demonstrate this process with an example below.
Suppose we have a C function that exchanges two integers, and its source code is as follows (save as):
#include <> void swap(int32_t* a, int32_t* b) { int32_t temp = *a; *a = *b; *b = temp; }
First, we need to compile this C file to generate a static library (in this example,):
gcc -c ar rcs
We can then create an external function interface in Rust to use this library. First, we need toAdd one to the file
Scripts and
libc
rely:
[package] #... build = "" [dependencies] libc = "0.2" [build-dependencies] cc = "1.0"
Then we're inThe script (I put it in the project root directory) tells cargo how to build our C library:
extern crate cc; fn main() { cc::Build::new() .file("") .compile(""); }
Next, we can create Rust's external function interface:
extern crate libc; extern "C" { fn swap(a: *mut i32, b: *mut i32); } fn main() { let mut x = 5; let mut y = 10; unsafe { swap(&mut x as *mut i32, &mut y as *mut i32); } println!("x: {}, y: {}", x, y); // x: 10, y: 5 }
In this code, we first importlibc
The library is used to obtain the integer definition in C language and then creates a name calledswap
The external function interface, finallyunsafe
Call this function in the block.
This is just a simple example, and the actual C and Rust interaction may involve more details such as error handling, memory management, etc.
Note1: ar rcs
This is a usear
Unix command line instructions that create static libraries. Let's analyze the various parts of this instruction:
ar
: This is a program used to create, modify and extract static libraries. It is a common tool in Unix environments.-
rcs
: This is passed toar
The parameters represent the following operations:-
r
: Insert a file or replace a file that already exists in the library. -
c
: Create a library if needed. -
s
: Create an index of the target file. This is for linking, which allows the linker to find functions and variables in the library faster.
-
: This is the name of the output file, which is the static library we want to create. By convention, the name of a static library is usually
lib
Start with.a
Finish.: This is the input file, that is, the target file to be added to the static library. It is usually generated by a C or C++ compiler.
so,ar rcs
This instruction means "create a name calledstatic library and
Add to this library”.
Note2: In C language,is a standard library header file that defines a set of integer types of exact widths.
int32_t
yesone of the types defined in. It is a 32-bit signed integer type with exact width. in other words,
int32_t
is an integer type that always takes up 32 bits (or 4 bytes) of space, no matter which platform it runs on. This makes it very useful because you can make sure that no matter which platform your code is compiled and run,int32_t
All have the same representation and behavior.
Similarly,Other exact width integer types are also defined, such as
int16_t
(16-bit signed integer)uint32_t
(32-bit unsigned integer) etc. These types provide predictable behavior across different platforms, which is very important in many cases, especially when you need to ensure the size and range of integers.
This is the end of this article about the implementation steps of Rust calling C programs. For more related contents of Rust calling C programs, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!