How do I use C preprocessor macros with Rust's FFI? -


i'm writing code interfaces existing library written in c. in rust code i'd able use values cpp macros. if have c include.h looks this:

#define init_flag 0x00000001 

i'd able use in rust this:

#[link(name="mylib")] extern {     pub static init_flag: c_int = init_flag; } 

i've looked @ other ffi code , see lot of people duplicating these values in rust instead of getting them ffi. seems little brittle, , i'd able handle more complicated things defined via cpp macros. running cpp on rust files work if i'm sure cpp macros used simple things.

it impossible, , don't think possible in future. c macros bring many problems them. if want run cpp on rust sources, can manually.

if don't want , if there lot of constants , don't want copy values c code rust can make c wrapper provide global variables these values:

#define init_flag 0x00000001  ...  const int init_flag = init_flag; 

you compile file, create static library , link usual:

$ gcc -c init_flag.c $ ar r libinitflag.a init_flag.o 

rust source:

use std::libc;  #[link(name="initflag", kind="static")] extern {     pub static init_flag: libc::c_int; } 

rust source identical tried achieve. need c glue object file, however.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -