wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

Guest C bindings - cast char * for void *

Open ricochet opened this issue 3 years ago • 2 comments

When generating a c binding and including it in a C++ project, there is an error and a warning. Are there any plans or desire to add a C++ generator? I found an easy workaround.

The following warning is fixed by renaming the c file generated to cpp:

clang-14: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]

This error is from additional strictness in C++: split.cpp:34:14: error: assigning to 'char *' from incompatible type 'void *'

A compatible C change is to do a cast:

#original:
# ret->ptr = canonical_abi_realloc(NULL, 0, 1, ret->len);

ret->ptr = (char *)(canonical_abi_realloc(NULL, 0, 1, ret->len));

wit

record subphrase {
  str: string,
  idx: s32
}
split-str: func(phrase: string, delim: string) -> list<subphrase>

We should add the cast to the c bindings.

ricochet avatar Jul 25 '22 20:07 ricochet

I agree that it'd be good to add that cast to the C bindings code, and make any other fixes needed so that the generated C can be compiled as C++.

It'd also be desirable to have an idiomatic C++ generator, which would use things like std::string instead of char *, but that's a larger project.

sunfishcode avatar Jul 25 '22 20:07 sunfishcode

I created a separate issue for the work on a C++ binding: https://github.com/bytecodealliance/wit-bindgen/issues/826

cpetig avatar Jan 30 '24 22:01 cpetig

Compiling the C bindings in C++ mode is now fixed and tested, as of https://github.com/bytecodealliance/wit-bindgen/pull/902.

sunfishcode avatar Mar 20 '24 23:03 sunfishcode