Windows上的libc crate不提供sprintf

时间:2019-08-21 13:02:03

标签: windows rust libc

我用Rust编写了一个库,该库使用由libc crate提供的sprintf函数使用C格式。一切都可以在macOS和Linux上运行,但由于sprintf函数不存在,因此无法在Windows上编译。

extern crate libc;
use libc::{c_char, c_double, sprintf};
let c_resfmt: CString =
    CString::new(c_resfmt).expect("Couldn't convert to c_resfmt to CString ");
let result_buffer: *mut c_char = CString::new("").expect("").into_raw();
unsafe {
    sprintf(result_buffer, c_resfmt.as_ptr(), scaled_result as c_double);
    let formatted_result = CString::from_raw(result_buffer).into_string().unwrap();
    ptr_result.with_result_value(formatted_result);
}
error[E0432]: unresolved import `libc::sprintf`
  --> src\stdf_helper\reader\mod.rs:12:13
   |
12 | use libc::{ sprintf, c_char, c_double };
   |             ^^^^^^^
   |             |
   |             no `sprintf` in the root
   |             help: a similar name exists in the module: `isprint`

是否有替代名称或替代名称?

1 个答案:

答案 0 :(得分:-3)

这可能是一个工具链。

您可能正在macOS和Linux(* Nix)上使用GNU工具链

确保您已安装并使用了最新,最出色的MSVC工具链

阅读this article了解更多信息

  

与C交互的两个问题之间的区别。   您正在使用由一个或另一个工具链构建的库,您需要   使其与适当的Rust工具链匹配。如果不确定,   使用MSVC;这是有充分理由的默认设置。

在Windows上使用MSVC。