将 C 函数指针传递给 emscripten 回调
Passing a C function pointer to emscripten callback
我正在使用带有 emscripten 的 SDL2 来玩一个小游戏。如果按如下方式重新加载或关闭浏览器选项卡,我正在尝试传递函数指针以释放一些内存:
emscripten_set_beforeunload_callback(0, on_before_onload);
on_before_onload 函数签名定义如下:
char *on_before_onload(int eventType, const void *reserved, void *userData);
我收到此警告:
incompatible function pointer types passing 'char *(int, const void *, void *)' to parameter of type 'em_beforeunload_callback' (aka 'const char *(*)(int, const void *, void *)') [-Wincompatible-function-pointer-types]
emscripten_set_beforeunload_callback(0, on_before_onload);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我是 C 的新手,显然还没有完全掌握传递函数指针。我试了很多不同的东西都无济于事。
它似乎调用了 Chrome 中的函数,但不是 Safari,即使编译器抱怨。
函数应该怎么定义?
错误告诉您 on_before_onload
的原型不正确。 According to this, 需要:
typedef const char *(*em_beforeunload_callback)(int eventType, const void *reserved, void *userData);
其中参数定义为
eventType (int) – The type of beforeunload event (EMSCRIPTEN_EVENT_BEFOREUNLOAD).
reserved (const void*) – Reserved for future use; pass in 0.
userData (void*) – The userData originally passed to the registration function.
- Return 输入
char *
,returns 一个要显示给用户的字符串。
使用上面创建的 typedefed 函数指针 (on_before_onload_fptr
) 在其使用范围内的代码位置创建 on_before_onload
,也许 main()
:
em_beforeunload_callback on_before_onload;
然后,在代码的其他地方,实际函数根据新类型定义:
char *on_before_onload(int eventType, const void *reserved, void *userData)
{
char *retBuf = NULL;
//code to handle event here
return NULL;
}
在将指针传递给函数之前,您应该像这样声明函数指针:
char * ( *on_before_onload_fptr )(int, void *, void *) = on_before_onload
我正在使用带有 emscripten 的 SDL2 来玩一个小游戏。如果按如下方式重新加载或关闭浏览器选项卡,我正在尝试传递函数指针以释放一些内存:
emscripten_set_beforeunload_callback(0, on_before_onload);
on_before_onload 函数签名定义如下:
char *on_before_onload(int eventType, const void *reserved, void *userData);
我收到此警告:
incompatible function pointer types passing 'char *(int, const void *, void *)' to parameter of type 'em_beforeunload_callback' (aka 'const char *(*)(int, const void *, void *)') [-Wincompatible-function-pointer-types]
emscripten_set_beforeunload_callback(0, on_before_onload);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我是 C 的新手,显然还没有完全掌握传递函数指针。我试了很多不同的东西都无济于事。
它似乎调用了 Chrome 中的函数,但不是 Safari,即使编译器抱怨。
函数应该怎么定义?
错误告诉您 on_before_onload
的原型不正确。 According to this, 需要:
typedef const char *(*em_beforeunload_callback)(int eventType, const void *reserved, void *userData);
其中参数定义为
eventType (int) – The type of beforeunload event (EMSCRIPTEN_EVENT_BEFOREUNLOAD).
reserved (const void*) – Reserved for future use; pass in 0.
userData (void*) – The userData originally passed to the registration function.
- Return 输入
char *
,returns 一个要显示给用户的字符串。
使用上面创建的 typedefed 函数指针 (on_before_onload_fptr
) 在其使用范围内的代码位置创建 on_before_onload
,也许 main()
:
em_beforeunload_callback on_before_onload;
然后,在代码的其他地方,实际函数根据新类型定义:
char *on_before_onload(int eventType, const void *reserved, void *userData)
{
char *retBuf = NULL;
//code to handle event here
return NULL;
}
在将指针传递给函数之前,您应该像这样声明函数指针:
char * ( *on_before_onload_fptr )(int, void *, void *) = on_before_onload