知识卡片

普通读书笔记卡 · 6074.h.1.a.1

外部声明约定签名与调用ABI

理解等级:中级

Rust调用C函数时,extern块描述签名和C ABI,链接阶段再匹配实际实现。声明正确性由编写者负责,编译通过并不验证C头文件一致。Rust2024要求unsafe extern块,块内项目也可按准确契约标safe或unsafe,不能把老版语法与新版混为一谈。

版本依据:Edition Guide外部块,2026-10-27查阅。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《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”.