kaisawind's blog
  • 关于
  • 所有帖子

rust中char在不同CPU类型错误 - Thu, Oct 15, 2020

rust中char在不同CPU类型错误

1. 概述

使用rust的bindgen链接C的链接库和rust语言。但是在不同平台中,c_char存储类型不同,导致编译错误。 ARM64(aarch64)中c_char使用u8进行存储,AMD64中使用i8进行存储。

2. 错误修改

.
.
   Compiling remacs v0.1.0 (/home/mmw/local/remacs/rust_src)
   Compiling remacs-macros v0.1.0 (/home/mmw/local/remacs/rust_src/remacs-macros)
error[E0308]: mismatched types
  --> src/callproc.rs:62:31
   |
62 |         unsafe { build_string(NULL_DEVICE.as_ptr() as *const i8) }.into()
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u8, found i8
   |
   = note: expected type `*const u8`
              found type `*const i8`

error[E0308]: mismatched types
  --> src/callproc.rs:69:13
   |
69 |             encoded_file.const_data_ptr() as *const i8,
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u8, found i8
   |
   = note: expected type `*const u8`
              found type `*const i8`

error[E0308]: mismatched types
  --> src/callproc.rs:78:17
   |
78 |                 "Opening process input file".as_ptr() as *const i8,
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u8, found i8
   |
   = note: expected type `*const u8`
              found type `*const i8`
.
.
.

github课题 https://github.com/remacs/remacs/issues/1393

解决方法: 不使用u8或i8类型,使用libc::c_char类型。

    // unsafe { build_string(NULL_DEVICE.as_ptr() as *const i8) }.into()
    unsafe { build_string(NULL_DEVICE.as_ptr() as *const libc::c_char) }.into()


辽ICP备2021007608号 | © 2025 | kaisawind

Facebook Twitter GitHub