rust中char在不同CPU类型错误

  1. 1. 概述
  2. 2. 错误修改

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()

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 wind.kaisa@gmail.com

💰

×

Help us with donation