Lesson 39 of 50 · rust
Foreign Function Interface (FFI)
Duration: 20 minutes
Lesson 39 of 50 · rust
Duration: 20 minutes
extern "C" blocks and #[link].\n\nc\n/* hello.c */\n#include <stdio.h>\n\nvoid hello_from_c() {\n printf(\"Hello from C!\\n\");\n}\n\n\nrust\n// Cargo.toml\n[build-dependencies]\ncc = \"1.0\"\n\n\nrust\n// build.rs\nfn main() {\n cc::Build::new().file("src/hello.c").compile(\"hello\");\n}\n\n\nrust\nextern \"C\" {\n fn hello_from_c();\n}\n\nfn main() {\n unsafe { hello_from_c(); }\n}\n\n