Doesn't handle `static const` structs correctly
Input C/C++ Header
struct foo {
int a;
int b;
};
static const struct foo FOO1 = {
.a = 12,
.b = 23,
};
Bindgen Invocation
$ bindgen input.h
Actual Results
/* automatically generated by rust-bindgen 0.54.1 */
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct foo {
pub a: ::std::os::raw::c_int,
pub b: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_foo() {
assert_eq!(
::std::mem::size_of::<foo>(),
8usize,
concat!("Size of: ", stringify!(foo))
);
assert_eq!(
::std::mem::align_of::<foo>(),
4usize,
concat!("Alignment of ", stringify!(foo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo>())).a as *const _ as usize },
0usize,
concat!("Offset of field: ", stringify!(foo), "::", stringify!(a))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo>())).b as *const _ as usize },
4usize,
concat!("Offset of field: ", stringify!(foo), "::", stringify!(b))
);
}
extern "C" {
pub static FOO1: foo;
}
Expected Results
I'm not really sure what I expected to happen, but creating a broken binding seems very misleading. The FOO1 static will not ever work correctly. In C that static const will not be available at link-time, so attempting to use the constant in Rust just creates a confusing link error.
input.7rcbfp3g-cgu.0:(.text._ZN5input4main17h59f421c58bec5f03E+0x7): undefined reference to `FOO1'
It would be great if bindgen could convert the struct literal into an equivalent Rust one, but I suspect that is difficult. Note that the bindings do work fine here for (some?) other types, if I make a static const int BAR = 12, it does generate pub const BAR: ::std::os::raw::c_int = 12;
Yeah, I don't think libclang gives us initializer information. If it does we should fix this relatively easily.
But probably we shouldn't generate the static anyways.
I also encountered this problem. I am generating bindings for NVIDIA Video Codec SDK and there are several const statics for various GUIDs. When I try to use them I get link errors:
nvidia_video_codec_sdk_rs-9d53b410ae856590.47xwz8gav15k51ja.rcgu.o : error LNK2019: unresolved external symbol NV_ENC_CODEC_H264_GUID referenced in function _ZN25nvidia_video_codec_sdk_rs5tests7example17ha0e72497c8f7a333E
nvidia_video_codec_sdk_rs-9d53b410ae856590.47xwz8gav15k51ja.rcgu.o : error LNK2019: unresolved external symbol NV_ENC_PRESET_LOW_LATENCY_HP_GUID referenced in function _ZN25nvidia_video_codec_sdk_rs5tests7example17ha0e72497c8f7a333E
nvidia_video_codec_sdk_rs-9d53b410ae856590.47xwz8gav15k51ja.rcgu.o : error LNK2019: unresolved external symbol NV_ENC_H264_PROFILE_HIGH_GUID referenced in function _ZN25nvidia_video_codec_sdk_rs5tests7example17ha0e72497c8f7a333E
bindgen 0.65.1
I also ran into this today. I'm trying to create bindings for a library that uses static const structs in header files for configuration. Not sure if that is a great design choice, but bindgen generates code that is broken :/
Trying to solve this by putting the configuration in rust and exporting it back led me to this other issue: https://github.com/rust-lang/rust-bindgen/issues/3125