mozilla / cbindgen

A project for generating C bindings from Rust code
Mozilla Public License 2.0
2.37k stars 306 forks source link

add forward declaration automatically using cbindgen prevent the compile warning #861

Open lyh169 opened 1 year ago

lyh169 commented 1 year ago

the rust code is :

#[repr(C)]
#[derive(Clone)]
pub struct querier_t {
    _private: [u8; 0],
}

#[repr(C)]
#[derive(Clone)]
pub struct Querier_vtable {
    pub get_call_info: extern "C" fn (
        *mut *mut GoQuerier,
    ) -> i32,
}

#[repr(C)]
#[derive(Clone)]
pub struct GoQuerier {
    pub state: *const querier_t,
    pub vtable: Querier_vtable,
}

the generate header file is:

typedef struct querier_t {
  uint8_t _private[0];
} querier_t;

typedef struct Querier_vtable {
  int32_t (*get_call_info)(struct GoQuerier**);
} Querier_vtable;

typedef struct GoQuerier {
  const struct querier_t *state;
  struct Querier_vtable vtable;
} GoQuerier;

when i used this header file, it warning like this :

warning: declaration of 'struct GoQuerier' will not be visible outside of this function [-Wvisibility]

so i need add the forward declaration in front of Querier_vtable like this : struct GoQuerier

How can i add this forward declaration automatically using cbindgen ?

the cbindgen.toml is:

# This is a template cbindgen.toml file with all of the default values.
# Some values are commented out because their absence is the real default.
#
# See https://github.com/eqrion/cbindgen/blob/master/docs.md#cbindgentoml
# for detailed documentation of every option here.

language = "C"

############## Options for Wrapping the Contents of the Header #################

 header = "/* (c) 2019 Confio OÜ. Licensed under Apache-2.0 */"
# trailer = "/* Text to put at the end of the generated file */"
# include_guard = "my_bindings_h"
autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
include_version = true
# namespace = "my_namespace"
namespaces = []
sys_includes = []
includes = []
no_includes = false

############################ Code Style Options ################################

braces = "SameLine"
line_length = 100
tab_width = 2
documentation_style = "auto"

############################# Codegen Options ##################################

style = "both"

[defines]
# "target_os = freebsd" = "DEFINE_FREEBSD"
# "feature = serde" = "DEFINE_SERDE"

[export]
include = [
    "GoError",
    "ErrnoValue",
]
exclude = []
# prefix = "CAPI_"
item_types = []
renaming_overrides_prefixing = false

[export.rename]

[export.body]

[fn]
rename_args = "None"
# must_use = "MUST_USE_FUNC"
# prefix = "START_FUNC"
# postfix = "END_FUNC"
args = "auto"

[struct]
rename_fields = "None"
# must_use = "MUST_USE_STRUCT"
derive_constructor = false
derive_eq = false
derive_neq = false
derive_lt = false
derive_lte = false
derive_gt = false
derive_gte = false

[enum]
rename_variants = "None"
# must_use = "MUST_USE_ENUM"
add_sentinel = false
prefix_with_name = false
derive_helper_methods = false
derive_const_casts = false
derive_mut_casts = false
# cast_assert_name = "ASSERT"
derive_tagged_enum_destructor = false
derive_tagged_enum_copy_constructor = false
private_default_tagged_enum_constructor = false

[const]
allow_static_const = true

[macro_expansion]
bitflags = false

############## Options for How Your Rust library Should Be Parsed ##############

[parse]
parse_deps = false
# include = []
exclude = []
clean = false
extra_bindings = []

[parse.expand]
crates = []
all_features = false
default_features = true
features = []