Hey guys, I'm currently trying to write a translation layer for the Steamworks SDK in pure C.
This SDK relies on C++ headers to link against, but has a C ABI included in the library. My goal has been to write a generator for a pure C header, including all the data structures needed to interface with the C ABI.
As part of my research for this project, I've run into an issue with calling C++ C ABI functions which return strings, illustrated in the example below:
// libabi.so
extern "C" {
const char* getMessage() {
return "Hello from the dynamic library!";
}
}
// abi_test.c
int main() {
const char *message = getMessage();
printf("Message: %s\n", message);
return 0;
}
(I have also tested this function returning a malloced char *, but the same problem occurs)
When running abi_test.c (properly linked against the .so), the memory at the pointer returned from getMessage() is corrupted.
(gdb) print message
$1 = 0x555592a0 <error: Cannot access memory at address 0x555592a0>
In my sanity checking, I tested creating a new C++ file to wrap the C++ ABI, and expose it as extern "C" within the same project as the test, and observed the memory being returned from the library un-corrupted in C++, and the same behavior with the pointer memory corrupting once it returns to C from the forward declaration.
I hope this isn't too much of a word soup, and I'm happy to clarify anything... This is a minimum recreated example of what I've experienced testing the Steamworks SDK directly, and if it helps I can upload a full CMake project example