[英]Undefined symbol when clang compiling to target wasm
所以我設法在瀏覽器中運行了一些簡單的 webassembly 應用程序,我設法從 Javascript 調用了 webassembly 代碼。 但是,當我嘗試從 C 代碼調用 Javascript 時,我無法編譯代碼
我正在使用 clang 9.0 和 wasi-libc。 我如何編譯:
clang --target=wasm32-wasi -Wl,--allow-undefined-file=src/wasm.syms -Wl,--export-all --sysroot wasm-test/wasi-libc/sysroot -O2 -s -o ./public/main.wasm ./src/main.cpp
src/main.cpp:
#include <stdio.h>
void __console_log(int str, int len);
int main()
{
printf("MAIN!\n");
return 0;
}
extern "C"
{
void test()
{
char *str = "TEST!";
__console_log((int)str, 5);
}
int test2()
{
return 10;
}
}
wasm.syms:
__console_log
如果我在 cpp 文件中刪除對__console_log
的引用它可以工作,但是當我添加它時,即使我在命令行中有--allow-undefined-file=src/wasm.syms
(嘗試使用 - U=__console_log 也是,同樣的錯誤):
wasm-ld: error: /var/folders/vl/4bjdkcfx1gncfx0vzrl0s57c0000gn/T/main-a723f9.o: undefined symbol: __console_log(int, int)
以下是使用-v
標志運行時的完整日志:
$ clang --target=wasm32-wasi -Wl,--allow-undefined-file=src/wasm.syms -Wl,--export-all --sysroot /Users/hoff/dev/wasm-test/wasi-libc/sysroot -O2 -s -o ./public/main.wasm ./src/main.cpp -v
clang version 9.0.0 (git://github.com/llvm/llvm-project.git 0399d5a9682b3cef71c653373e38890c63c4c365)
Target: wasm32-unknown-wasi
Thread model: posix
InstalledDir: /Users/hoff/dev/wasm-test/clang/bin
"/Users/hoff/dev/wasm-test/clang/bin/clang-9" -cc1 -triple wasm32-unknown-wasi -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model static -mthread-model posix -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu generic -fvisibility hidden -dwarf-column-info -debugger-tuning=gdb -target-linker-version 512.4 -momit-leaf-frame-pointer -v -resource-dir /Users/hoff/dev/wasm-test/clang/lib/clang/9.0.0 -isysroot /Users/hoff/dev/wasm-test/wasi-libc/sysroot -internal-isystem /Users/hoff/dev/wasm-test/wasi-libc/sysroot/include/wasm32-wasi/c++/v1 -internal-isystem /Users/hoff/dev/wasm-test/wasi-libc/sysroot/include/c++/v1 -internal-isystem /Users/hoff/dev/wasm-test/clang/lib/clang/9.0.0/include -internal-isystem /Users/hoff/dev/wasm-test/wasi-libc/sysroot/include/wasm32-wasi -internal-isystem /Users/hoff/dev/wasm-test/wasi-libc/sysroot/include -O2 -fdeprecated-macro -fdebug-compilation-dir /Users/hoff/dev/wasm-test -ferror-limit 19 -fmessage-length 102 -fobjc-runtime=gnustep -fcxx-exceptions -fexceptions -fno-common -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /var/folders/vl/4bjdkcfx1gncfx0vzrl0s57c0000gn/T/main-a723f9.o -x c++ ./src/main.cpp
clang -cc1 version 9.0.0 based upon LLVM 9.0.0 default target x86_64-apple-darwin17.6.0
ignoring nonexistent directory "/Users/hoff/dev/wasm-test/wasi-libc/sysroot/include/wasm32-wasi/c++/v1"
ignoring nonexistent directory "/Users/hoff/dev/wasm-test/wasi-libc/sysroot/include/c++/v1"
ignoring nonexistent directory "/Users/hoff/dev/wasm-test/wasi-libc/sysroot/include/wasm32-wasi"
#include "..." search starts here:
#include <...> search starts here:
/Users/hoff/dev/wasm-test/clang/lib/clang/9.0.0/include
/Users/hoff/dev/wasm-test/wasi-libc/sysroot/include
End of search list.
./src/main.cpp:17:21: warning: ISO C++11 does not allow conversion from string literal to 'char *'
[-Wwritable-strings]
char *str = "TEST!";
^
1 warning generated.
"/Users/hoff/dev/wasm-test/clang/bin/wasm-ld" --strip-all -L/Users/hoff/dev/wasm-test/wasi-libc/sysroot/lib/wasm32-wasi /Users/hoff/dev/wasm-test/wasi-libc/sysroot/lib/wasm32-wasi/crt1.o --allow-undefined-file=src/wasm.syms --export-all /var/folders/vl/4bjdkcfx1gncfx0vzrl0s57c0000gn/T/main-a723f9.o -lc /Users/hoff/dev/wasm-test/clang/lib/clang/9.0.0/lib/wasi/libclang_rt.builtins-wasm32.a -o ./public/main.wasm
wasm-ld: error: /var/folders/vl/4bjdkcfx1gncfx0vzrl0s57c0000gn/T/main-a723f9.o: undefined symbol: __console_log(int, int)
clang-9: error: linker command failed with exit code 1 (use -v to see invocation)
任何幫助表示贊賞。
您還需要將__console_log
聲明為 extern "C"。 該線索在於錯誤消息中包含函數簽名這一事實。 lld 在這里要求 C++ 符號(如果沒有,也許會更清楚,我不確定)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.