首页 > 解决方案 > 如何在 Emscripten 中从 MAIN_MODULE 正确加载 SIDE_MODULE?

问题描述

我正在尝试使用MAIN_MODULE/SIDE_MODULE基于https://github.com/emscripten-core/emscripten/wiki/Linking#general-dynamic-linking的简单示例,但无法使其工作。

这是我的代码:

主.c:

#include <stdio.h>
 
int side(int a);
 
int main()
{
    printf("hello world %d\n", side(1));
}

边.c:

int side(int a)
{
    return a + 1;
}

编译:

emcc side.c -s SIDE_MODULE=1 -o side.wasm
emcc main.c -s MAIN_MODULE=1 -o main.html

索引.html

<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <script>
    var Module
      = {
      preRun: []
      , postRun: []
      , print: function (e) {
        1 < arguments.length && (e = Array.prototype.slice.call(arguments).join(" "));
        console.log(e);
      }
      , printErr: function (e) {
        1 < arguments.length && (e = Array.prototype.slice.call(arguments).join(" "));
        console.error(e)
      }
    };
    Module.dynamicLibraries = ['side.wasm'];
  </script>
  <script async src="main.js"></script>
</body>

</html>

如果我用 运行它python -m SimpleHTTPServer,我会收到以下错误:

(index):19 Assertion failed: undefined
printErr @ (index):19
abort @ main.js:33157
assert @ main.js:1074
removeRunDependency @ main.js:1984
(anonymous) @ main.js:2274
Promise.then (async)
instantiateArrayBuffer @ main.js:2272
(anonymous) @ main.js:2301
Promise.then (async)
(anonymous) @ main.js:2296
Promise.then (async)
instantiateAsync @ main.js:2288
createWasm @ main.js:2321
Module.asm @ main.js:2344
(anonymous) @ main.js:31495
(index):19 failed to asynchronously prepare wasm: abort(Assertion failed: undefined) at Error
    at jsStackTrace (http://localhost:8000/main.js:1626:13)
    at stackTrace (http://localhost:8000/main.js:1643:12)
    at abort (http://localhost:8000/main.js:33163:44)
    at assert (http://localhost:8000/main.js:1074:5)
    at removeRunDependency (http://localhost:8000/main.js:1984:5)
    at http://localhost:8000/main.js:2274:7

任何建议或指导将不胜感激!

标签: cemscripten

解决方案


推荐阅读