我应该使用 libc::c_char 还是 std::os::raw::c_char?

Should I use libc::c_char or std::os::raw::c_char?

我正在为 Rust 编写 FFI 包装器。我见过 libc::c_charstd::os::raw::c_char 的用法。我对 C 的了解非常少,我想知道是否有任何区别。如果我想通过 cffi 将字符串公开给 Python,我应该使用什么?

我无法回答哪个更地道,但我可以说它们在 64 位 Linux(大多数在线托管文档的默认平台)上是相同的:

std::os::raw::c_char:

type c_char = i8;

libc::c_char:

type c_char = i8;

更广泛地跨不同平台进行查看有点复杂。标准库 tightly groups all the definitions for c_char, but libc groups them by platform。考虑到这种类型的基础性,我希望它们在所有平台上都相同。

实际上,这两个定义都不太可能改变,因此可能没有任何稳定性差异。

我的意见是使用标准库版本中的那个,直到我需要使用 libc 中的特定内容,在这种情况下,我可能会将所有类型切换到 libc 变体,只是为了始终如一。

除了@Shepmaster 的回答,我想强调一个事实,即 libc 不依赖于 std

因此,如果您不能使用 std,则必须使用 libc。 这种情况可见here.

Currently libc by default links to the standard library, but if you would instead like to use libc in a #![no_std] situation or crate you can request this via:

[dependencies]
libc = { version = "0.2", default-features = false }