Add C to Rust example
This commit is contained in:
8
c-to-rust/Cargo.toml
Normal file
8
c-to-rust/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "c-to-rust"
|
||||
version = "0.1.0"
|
||||
authors = ["Alex Crichton <alex@alexcrichton.com>"]
|
||||
|
||||
[lib]
|
||||
name = "double_input"
|
||||
crate-type = ["staticlib"]
|
||||
18
c-to-rust/Makefile
Normal file
18
c-to-rust/Makefile
Normal file
@@ -0,0 +1,18 @@
|
||||
all: target/double
|
||||
target/double
|
||||
|
||||
target:
|
||||
mkdir -p $@
|
||||
|
||||
target/double: target/main.o target/libdouble_input.a
|
||||
$(CC) -o $@ -Wl,--gc-sections $^ -lpthread
|
||||
|
||||
target/libdouble_input.a: src/lib.rs Cargo.toml
|
||||
cargo build
|
||||
(cd target && ln -nsf debug/libdouble_input-*.a libdouble_input.a)
|
||||
|
||||
target/main.o: src/main.c | target
|
||||
$(CC) -o $@ -c $<
|
||||
|
||||
clean:
|
||||
rm -rf target
|
||||
6
c-to-rust/src/lib.rs
Normal file
6
c-to-rust/src/lib.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
#![crate_type = "staticlib"]
|
||||
|
||||
#[no_mangle]
|
||||
pub extern fn double_input(input: i32) -> i32 {
|
||||
input * 2
|
||||
}
|
||||
11
c-to-rust/src/main.c
Normal file
11
c-to-rust/src/main.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern int32_t double_input(int32_t input);
|
||||
|
||||
int main() {
|
||||
int input = 4;
|
||||
int output = double_input(input);
|
||||
printf("%d * 2 = %d\n", input, output);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user