From 7865085c4477c0233cf07519af2b217365567a66 Mon Sep 17 00:00:00 2001 From: zy Date: Fri, 8 Sep 2023 03:48:44 +0000 Subject: ffi hello world --- .devcontainer/devcontainer.json | 35 +++++++++++++++++++++++++++++++++++ .gitignore | 1 + Cargo.lock | 26 ++++++++++++++++++++++++++ Cargo.toml | 13 +++++++++++++ build.rs | 3 +++ sample.c | 3 +++ src/main.rs | 11 +++++++++++ 7 files changed, 92 insertions(+) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 build.rs create mode 100644 sample.c create mode 100644 src/main.rs diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..b9f3ab8 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,35 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/rust +{ + "name": "ffi_demo", + // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile + // "image": "mcr.microsoft.com/devcontainers/rust:0-1-bullseye", rust:slim-bookworm + "image": "mcr.microsoft.com/devcontainers/rust:0-1-bullseye", + "runArgs": [ + "--network=host" + ], + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + // Configure tool-specific properties. + "customizations": { + // Configure properties specific to VS Code. + "vscode": { + "settings": { + "http.proxySupport": "override", + "http.proxy": "http://172.28.240.1:7890" + }, + "extensions": [ + "streetsidesoftware.code-spell-checker", + "GitHub.copilot", + "GitHub.copilot-chat", + "eamodio.gitlens" + ] + } + }, + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "rustc --version", + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..77bc8ac --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,26 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cc" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] + +[[package]] +name = "ffi_demo" +version = "0.1.0" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "libc" +version = "0.2.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..483ce07 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "ffi_demo" +version = "0.1.0" +edition = "2021" +build = "build.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[build-dependencies] +cc = "1.0.79" + +[dependencies] +libc = "0.2.0" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..705dedd --- /dev/null +++ b/build.rs @@ -0,0 +1,3 @@ +fn main() { + cc::Build::new().file("sample.c").compile("sample"); +} \ No newline at end of file diff --git a/sample.c b/sample.c new file mode 100644 index 0000000..c73ab02 --- /dev/null +++ b/sample.c @@ -0,0 +1,3 @@ +int add(int a,int b){ + return a+b; +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..7f69b07 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,11 @@ +use std::os::raw::c_int; + +#[link(name = "sample")] +extern "C" { + fn add(a: c_int, b: c_int) -> c_int; +} + +fn main() { + let r = unsafe { add(2, 18) }; + println!("{:?}", r); +} -- cgit v1.2.3