wasm中使用动态库
文章目录
先编译动态库
#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
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论