Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guest C bindings - cast char * for void * #290

Closed
ricochet opened this issue Jul 25, 2022 · 3 comments
Closed

Guest C bindings - cast char * for void * #290

ricochet opened this issue Jul 25, 2022 · 3 comments
Labels
gen-c Related to the C code generator

Comments

@ricochet
Copy link

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.

@sunfishcode
Copy link
Member

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.

@alexcrichton alexcrichton added the gen-c Related to the C code generator label Sep 6, 2022
@cpetig
Copy link

cpetig commented Jan 30, 2024

I created a separate issue for the work on a C++ binding: #826

@sunfishcode
Copy link
Member

Compiling the C bindings in C++ mode is now fixed and tested, as of #902.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gen-c Related to the C code generator
Projects
None yet
Development

No branches or pull requests

4 participants