先编译动态库

#define SIZE 5000
char dummy[SIZE] = {};

int side(void)
{
    return SIZE;
}

emcc side.c -s SIDE_MODULE=1 -o side.wasm

编译调用模块

#include <stdio.h>
#include <dlfcn.h>

int main() 
{
    void *handle;
    typedef int (*func_t)(void);

    handle = dlopen("side.wasm", RTLD_NOW);

    if (!handle) {
        printf("failed to open the library\n");
        return 0;
    }
    func_t func = (func_t)dlsym(handle, "side");

    if (!func) {
        printf("failed to find the method\n");
        dlclose(handle);
        return 0;
    }
    printf("side module size: %d byte\n", func());
}

emcc main.c -s MAIN_MODULE=1 -o main.html

<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <script>
    var Module = {
      dynamicLibraries : ["side.wasm"],
    };
    </script>
  <script async src="main.js"></script>
</body>

</html>

就可以使用动态库了,不受4k限制
参考了
https://stackoverflow.com/questions/63150966/how-to-dlopen-a-side-module-larger-than-4kb


o_ra
6 声望4 粉丝

记录工作中碰到的问题


引用和评论

0 条评论