CCALL和CWARP功能在Emscripten中的目的是什么?
我正在按照emscripten文档的说明在这里,我想以最小的例子进行练习。我的最终目标是创建一个C ++库,将其编译到单个WASM二进制文件中,然后在Pure-Frontend Web应用程序中使用这些编译的方法。
当您可以直接从“模块”中对其进行分组时,我不明白用“ ccall”调用C ++方法的目的。例如,我可以使用module._doubleNumber(9);
,它比module.ccall('DoubleNumber','number',['number'],[9])短。 ;
。
打电话给一个或另一个之间有什么区别?
这是完整的代码:
extern "C" {
int doubleNumber( int x ) {
int res = 2*x;
return res;
}
}
与
emcc main.cpp -o main.js -sEXPORTED_FUNCTIONS=_doubleNumber -sEXPORTED_RUNTIME_METHODS=ccall
我使用的简单HTML文档一起测试结果:
<html>
<head>
<meta charset="utf-8">
<title>WASM Demo</title>
</head>
<body>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript">
Module.ccall('doubleNumber','number', ['number'], [9]); // 18
Module._doubleNumber(9); // 18
</script>
</body>
</html>
I'm following the instructions from the Emscripten documentation here, and I want to practice with minimal examples. My final goal is to create a C++ library, compile it to a single wasm binary file and then use these compiled methods in pure-frontend web application.
I do not understand what is the purpose of calling C++ methods with "ccall" when you can just instatiate them directly from "Module". For example, I can use Module._doubleNumber(9);
, which is shorter than Module.ccall('doubleNumber','number', ['number'], [9]);
.
Whats the difference between calling one or another?
Here is the complete code:
extern "C" {
int doubleNumber( int x ) {
int res = 2*x;
return res;
}
}
Compiled with
emcc main.cpp -o main.js -sEXPORTED_FUNCTIONS=_doubleNumber -sEXPORTED_RUNTIME_METHODS=ccall
I'm using a simple html document to test results:
<html>
<head>
<meta charset="utf-8">
<title>WASM Demo</title>
</head>
<body>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript">
Module.ccall('doubleNumber','number', ['number'], [9]); // 18
Module._doubleNumber(9); // 18
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样的小例子没有区别。大型项目可能会有问题。
module.ccall
可以使用参数async = true
执行异步调用,而呼叫对导出的函数module._doubleenumber
始终是同步的。_DoubleNumber
的缩小名称将更改为另一个名称,通常是不可预测的,并且module._doubleenumber
将不确定。-sexported_functions = _DoubleNumber
可防止名称缩小,可用于小型C库。一个大C库将需要exported_functions
中列出的大量项目,您应考虑有限的命令行长。There is no difference in such the tiny example. There might be issues in big projects.
Module.ccall
can perform async call with the parameterasync=true
, whereas call to the exported functionModule._doubleNumber
is always sync._doubleNumber
will be changed to another name, usually unpredictable one, andModule._doubleNumber
will be undefined.-sEXPORTED_FUNCTIONS=_doubleNumber
prevents name minifying and can be used for small C libraries. A big C library would require a ton of items listed inEXPORTED_FUNCTIONS
, and you shall take into account the limited command line length.