Interfacing with C: A Simple Adder in Rust
This challenge focuses on a fundamental aspect of Rust interoperability: calling C functions from Rust. You'll implement a simple Rust program that uses an extern function written in C to perform addition. This is a common scenario when integrating Rust with existing C codebases or leveraging C libraries.
Problem Description
Your task is to create a Rust program that calls a C function to add two integers. You will define a Rust function marked with extern "C" that corresponds to a C function. You will then need to compile a separate C file containing the actual addition logic and link it with your Rust code.
Key Requirements:
- Rust
externFunction Declaration: Declare a Rust function signature that matches a C function accepting twointarguments and returning anint. - C Implementation: Write a C source file (
adder.c) that defines a function with the same signature as declared in Rust, which performs addition. - Rust Program Logic: In your Rust code (
main.rs), call the declaredexternfunction with sample integer values. - Output: Print the result of the addition returned by the C function.
Expected Behavior:
When the Rust program is executed, it should successfully call the C function, perform the addition, and print the correct sum to the console.
Edge Cases:
- Consider how different integer types might be handled (though for this basic problem,
intis sufficient). - Ensure correct compilation and linking steps are understood.
Examples
Example 1: Input:
- Rust program calls the
addfunction witha = 5,b = 7. - C
adder.ccontainsint add(int x, int y) { return x + y; }.
Output:
The sum is: 12
Explanation:
The Rust program declares an extern "C" function add. It then calls this function with 5 and 7. The call is routed to the C implementation, which returns 12. The Rust program receives this value and prints it.
Example 2: Input:
- Rust program calls the
addfunction witha = -10,b = 3. - C
adder.ccontainsint add(int x, int y) { return x + y; }.
Output:
The sum is: -7
Explanation: This demonstrates that the C function correctly handles negative numbers as expected for integer addition.
Constraints
- The C function and Rust
externfunction must both accept and return standardinttypes. - The C implementation should be provided in a file named
adder.c. - The Rust code should be in a file named
main.rs. - Compilation should result in a single executable.
Notes
- To compile this, you will need a C compiler (like GCC or Clang) and the Rust toolchain.
- The compilation process typically involves compiling the C code into an object file and then linking that object file with your Rust program. For example, using
gccandrustc:gcc -c adder.c(createsadder.o)rustc main.rs adder.o(creates an executable)
- Remember that Rust's type system is strict. Ensure the
externdeclaration in Rust precisely matches the C function signature in terms of argument types and return types. - The
#[link(name = "adder")]attribute can also be used in Rust to specify external libraries, but for a simple C file, direct linking during compilation is often more straightforward for learning. In this case, we'll link directly.