1

如果我使用 emscripten 接口,EM_JS符号似乎不会被导出或任何东西。

如果我使用以下 C 代码:

#include <emscripten.h>
#include <stdio.h>

//Function in Javascript Land
extern void writeout( int o );

EM_JS(void, console_logger, (const char* str), {
         console.log(UTF8ToString(str));
} );

void testcallback( int z )
{
        char ct[100];
        sprintf( ct, "Calling back: %d\n", z );
        console_logger(ct);
        writeout( z+10000 );
}

如果我尝试使用以下行进行编译,就像这样,这是我的问题。

/home/cnlohr/git/emsdk/upstream/emscripten/emcc -o add2.wasm add2.c -s EXPORTED_FUNCTIONS='["_add2","_testcallback","_console_logger"]' -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' -s ERROR_ON_UNDEFINED_SYMBOLS=0
warning: undefined symbol: writeout (referenced by top-level compiled C/C++ code)
emcc: error: undefined exported function: "_console_logger" [-Wundefined] [-Werror]

我可以尝试在没有它的情况下进行编译,如下所示:

/home/cnlohr/git/emsdk/upstream/emscripten/emcc -o add2.wasm add2.c -s EXPORTED_FUNCTIONS='["_add2","_testcallback"]' -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' -s ERROR_ON_UNDEFINED_SYMBOLS=0
warning: undefined symbol: writeout (referenced by top-level compiled C/C++ code)
cat add2.wasm | base64 | ./strencode  > add2.wasm.b64

当我加载它时,我得到

Uncaught (in promise) LinkError: import object field 'console_logger' is not a Function

如何让 emcc 实际使用我生成的函数EM_JS

仅供参考,这是我正在使用的 emcc 版本:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 2.0.0
clang version 12.0.0 (/b/s/w/ir/cache/git/chromium.googlesource.com-external-github.com-llvm-llvm--project a3036b386383f1c1e9d32c2c8dba995087959da3)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/cnlohr/git/emsdk/upstream/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Candidate multilib: .;@m64
Selected multilib: .;@m64
shared:INFO: (Emscripten: Running sanity checks)
4

1 回答 1

1

因此,对此的答案是,如果您以 .js 文件为目标,则这些函数(以及其他函数)将放在该单独的 js 文件中。如果您不想使用整个文件,则可以检查并随意复制内容。

于 2020-08-16T00:37:26.630 回答