檔案層次結構:
src:
main.c
main.html
main.js
out:
main.wasm -> compiled file
主.c:
#define WASM_EXPORT __attribute__((visibility("default")))
WASM_EXPORT
float power(float number,float power){
float res = number;
for (int i = 0; i < power - 1 ; i ) {
res = res * number;
}
return res;
}
主.js:
fetch('../out/main.wasm').then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes)
).then(results => {
instance = results.instance;
document.getElementById("container").textContent = instance.exports.power(2,8);
}).catch(console.error);
錯誤:
TypeError: instance.exports.power is not a function
剛開始wasm & 不知道為什么看不到電源功能
uj5u.com熱心網友回復:
您如何將 C 編譯為 wasm BTW?
一般來說,僅僅設定可見性是不夠的。您要么需要傳遞--export=power
給聯結器(很可能通過-Wl,--export=power
傳遞給編譯器),要么設定“export_name”屬性:__attribute__((export_name("power")))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/427872.html
上一篇:Sizeof與C中的變數
下一篇:理解C中回圈的數學