简体   繁体   English

包含标准库时的WebAssembly LinkError

[英]WebAssembly LinkError When Standard Library is Included

This very simple WebAssembly program works fine for me: 这个非常简单的WebAssembly程序对我来说很好:

test.c test.c的

int testing(void) {
    return 10;
}
int main(void) {
    return 0;
}

test.html 的test.html

<html>
    <body>
        <script>
            var imports = {};

            function instantiate(bytes, imports) {
                return WebAssembly.compile(bytes).then(m => new WebAssembly.Instance(m, imports));
            }
            fetch('test.wasm').then(response => response.arrayBuffer())
                .then(bytes => instantiate(bytes, imports))
                .then(instance => { 
                    console.log(instance.exports._testing());
                } );

        </script>
    </body>
</html>

I'm using: 我正在使用:

emcc test.c something.c -s "EXPORTED_FUNCTIONS=['_testing']" -s WASM=1 -O3 -o test.wasm

But then, if I try something like: 但是,如果我尝试类似的方法:

test.c test.c的

#include <stdio.h>
int testing(void) {
    printf("Hello!\n");
    return 10;
}
int main(void) {
    return 0;
}

First, it produces this error: 首先,它会产生以下错误:

TypeError: import object field 'env' is not an Object

Which I've tried to solve by adding an env field to imports : 我试图通过在imports添加一个env字段来解决该问题:

var imports = {
    env: {
        memoryBase: 0,
        tableBase: 0,
        memory: new WebAssembly.Memory({
            initial: 512
        }),
        table: new WebAssembly.Table({
            initial: 0,
            element: 'anyfunc'
        })
    }
};

But this just gives me another error: 但这给了我另一个错误:

LinkError: import object field '___syscall146' is not a Function

I've also tried using -s EXPORT_ALL=1 , but that just slightly changed the error message: 我也尝试使用-s EXPORT_ALL=1 ,但这只是稍微改变了错误消息:

LinkError: import object field '___setErrNo' is not a Function

I don't really know much about WebAssembly, so I'm not sure what's going on here. 我对WebAssembly的了解不多,所以我不确定这里发生了什么。 What exactly is causing this error? 究竟是什么导致此错误?

Edit: 编辑:

Interestingly, I don't get any error at all if I call malloc : 有趣的是,如果我调用malloc ,我根本不会收到任何错误:

#include <stdlib.h>
int testing(void) {
    int* p = malloc(5);
    *p = 17;
    free(p);
    return 7;
}
int main(void) {
    return 0;
}

But if I return values from any allocated memory: 但是,如果我从任何分配的内存中返回值:

#include <stdlib.h>
int testing(void) {
    int* p = malloc(5);
    *p = 17;
    free(p);
    return *p;
}
int main(void) {
    return 0;
}

I get this again: 我再次得到这个:

LinkError: import object field '___setErrNo' is not a Function

From the error message it seems like emcc isn't linking the standard library (maybe?), but I can't find anyone else with the same problem... 从错误消息看来,好像emcc没有链接标准库(也许?),但是我找不到其他人emcc相同的问题...

I finally got it to work by getting emcc to output a .js file directly: 我终于通过让emcc直接输出.js文件来实现它:

test.c test.c的

#include <stdio.h>

void testing(void) {
    printf("Hello!\n");
}

int main(void) {
    return 0;
}

test.html 的test.html

<html>
  <body>
    <script src="test.js"></script>
    <script>
        Module.onRuntimeInitialized = function() {
            _testing();
        }
    </script>

  </body>
</html>
emcc test.c -s EXPORTED_FUNCTIONS=['_testing'] -s WASM=1 -O3 -o test.js

I'm still not entirely sure what was wrong before... but at least it's working now. 我仍然不完全确定以前哪里出了问题...但是至少现在可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM