知识卡片
外部声明约定签名与调用ABI
Rust调用C函数时,extern块描述签名和C ABI,链接阶段再匹配实际实现。声明正确性由编写者负责,编译通过并不验证C头文件一致。Rust2024要求unsafe extern块,块内项目也可按准确契约标safe或unsafe,不能把老版语法与新版混为一谈。
版本依据:Edition Guide外部块,2026-10-27查阅。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 6. Beyond Standard Rust Invoking C Functions from Rust · 查看原书依据
extern "C" { pub fn add(x: c_int, y: c_int) -> c_int; } The declaration is marked as extern "C" to indicate that an external C library will provide the code for the function.5 The extern "C" marker also automatically marks the function as no_mangle, which we explore in “Name mangling”.
参考来源
- 位置:《Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)》第6章《Invoking C Functions from Rust》(spine10;extracted/Effective Rust 35 Specific Ways to Improve Your Rust Code (David Drysdale)/_epub-src/OEBPS/ch06.html;u174-ch06-007)。
- 结论依据:原文在“extern "C"”附近的定义、例子或代码支撑本卡核心说明:Rust调用C函数时,extern块描述签名和C ABI,链接阶段再匹配实际实现。边界按已读前提解释;示例未执行。
- 原始内容:extern "C" { pub fn add(x: c_int, y: c_int) -> c_int; } The declaration is marked as extern "C" to indicate that an external C library will provide the code for the function.5 The extern "C" marker also automatically marks the function as no_mangle, which we explore in “Name mangling”.