[英]Calling javascript function in c++ with emscripten embind
This question comes in two parts.这个问题分为两部分。 What I want to do is to put most of my program logic in c++ classes and some view related function in js (like DOM manipulation and styling.) I use emscripten embind the classes and it works fine while I don't know how to interact with hte js code (there are really limited resources on their tutorial.)
我想要做的是将我的大部分程序逻辑放在 c++ 类中,并将一些与视图相关的函数放在 js 中(如 DOM 操作和样式)。我使用 emscripten 来绑定这些类,它运行良好,但我不知道如何交互使用 hte js 代码(他们的教程资源非常有限。)
I was thinking to pass a val object to the c++ class according to their tutorial ( https://github.com/kripken/emscripten/wiki/Tutorial ) The passing works just fine while the "call" function doesn't work.我想根据他们的教程( https://github.com/kripken/emscripten/wiki/Tutorial )将 val 对象传递给 c++ 类,传递工作正常,而“调用”函数不起作用。 I got a compile time error
我有一个编译时错误
Here is the example I tried which they put on their tutorial这是我试过的例子,他们放在他们的教程中
#include <emscripten/val.h>
using namespace emscripten;
int main(){
val Math = val::global("Math");
Math.call("abs",-10);
return 0;
}
and I got the following errors: error: no matching member function for call to 'call' Math.call("abs",-10);我收到以下错误:错误:没有匹配的成员函数用于调用'call' Math.call("abs",-10); ~~~~^~~~ emscripten/1.5.6/system/include/emscripten/val.h:247:21: note: candidate template ignored: couldn't infer template argument 'ReturnValue' ReturnValue call(const char* name, Args&&... args) const {
~~~~^~~~ emscripten/1.5.6/system/include/emscripten/val.h:247:21: 注意:候选模板被忽略:无法推断模板参数 'ReturnValue' ReturnValue call(const char* name) , Args&&... args) const {
Basically it says the the compiler doesn't know the return type of the "call" function.基本上它说编译器不知道“调用”函数的返回类型。 Did I do anything wrong or is there a better way to interact with js code?
我做错了什么还是有更好的方式与 js 代码交互?
Thanks, yi谢谢,易
That's a common C++ problem.这是一个常见的 C++ 问题。 As a general rule, the following message should always make you double check in C++:
作为一般规则,以下消息应始终让您仔细检查 C++:
note: candidate template ignored: couldn't infer template argument 'ReturnValue' ReturnValue call(const char* name, Args&&... args) const
This mostly means that you tried to call a templated function but did not specify the necessary types.这主要意味着您尝试调用模板化函数但没有指定必要的类型。
If you look at the signature (in system/include/emscripten/val.h
):如果您查看签名(在
system/include/emscripten/val.h
):
template<typename ReturnValue, typename... Args>
ReturnValue call(const char* name, Args&&... args) const
While it can infer Args
quite fine, it has no idea, what ReturnValue
might be.虽然它可以很好地推断
Args
,但它不知道ReturnValue
可能是什么。 So calling this function should be done via eg:所以调用这个函数应该通过例如:
Math.call<int>("abs",-10);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.