Guest C bindings - cast char * for void *
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.
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.
I created a separate issue for the work on a C++ binding: https://github.com/bytecodealliance/wit-bindgen/issues/826
Compiling the C bindings in C++ mode is now fixed and tested, as of https://github.com/bytecodealliance/wit-bindgen/pull/902.