一、在vue中调用c函数
https://emscripten.org/docs/compiling/WebAssembly.html
WebAssembly默认情况下是异步编译,这意味着您必须先等待编译完成,然后才能调用已编译的代码(通过等待main(),或onRuntimeInitialized回调等)。您可以通过设置来关闭异步编译WASM_ASYNC_COMPILATION=0,但是由于当前的限制,该设置可能无法在Chrome中运行。
关闭了异步编译,获取WebAssembly二进制文件也可能需要异步操作(因为Web不允许在主线程上下载同步二进制文件)。
二、在webworker中调用c函数
webwoker:
(function (event) {
importScripts('test.js');
onmessage = function (event)
{
var eventData = event.data;
var res = 0;
switch (eventData.command)
{
case "testThread":
res = Module._testThread();
if(res)
{
postMessage({'function': "testThreadFailed", 'errorCode': res});
}
break;
default:
break;
}
};
})();
test.c
#include <emscripten.h>
#include <stdio.h>
// 实现一个加法
#ifdef __cplusplus
extern "C"{
#endif
int add(int a,int b) {
print