summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.devcontainer/devcontainer.json35
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock26
-rw-r--r--Cargo.toml13
-rw-r--r--build.rs3
-rw-r--r--sample.c3
-rw-r--r--src/main.rs11
7 files changed, 92 insertions, 0 deletions
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);
+}