CCALL和CWARP功能在Emscripten中的目的是什么?

发布于 2025-01-25 08:04:26 字数 1457 浏览 2 评论 0原文

我正在按照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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2025-02-01 08:04:26

这样的小例子没有区别。大型项目可能会有问题。

  1. 不太重要。 module.ccall可以使用参数async = true执行异步调用,而呼叫对导出的函数module._doubleenumber始终是同步的。
  2. 更重要。在较高的优化级别(-O2及更高)下,闭合编译器运行并减小(更改)功能名称。 _DoubleNumber的缩小名称将更改为另一个名称,通常是不可预测的,并且module._doubleenumber将不确定。
  3. -sexported_functions = _DoubleNumber可防止名称缩小,可用于小型C库。一个大C库将需要exported_functions中列出的大量项目,您应考虑有限的命令行长。

There is no difference in such the tiny example. There might be issues in big projects.

  1. Less important. Module.ccall can perform async call with the parameter async=true, whereas call to the exported function Module._doubleNumber is always sync.
  2. More important. At higher optimisation levels (-O2 and above), the closure compiler runs and minifies (changes) function names. A minified name for _doubleNumber will be changed to another name, usually unpredictable one, and Module._doubleNumber will be undefined.
  3. -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 in EXPORTED_FUNCTIONS, and you shall take into account the limited command line length.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文