为什么在每个导出的WASM函数中都调用对象的构造函数?
我正在为WASM模块编译一些C ++
struct Test{
Test(){
printf("Constructed");
}
};
Test t;
EXPORT void init(){...}
EXPORT void update(){...}
EXPORT void uninit(){...}
,我希望该构造函数仅用于t,但是查看Chrome的调试器表明,T的构造函数在每个导出函数的顶部都称为t的构造函数(func18是构造函数):
...
(func $init (;23;) (export "init")
(local $var0 i32) (local $var1 i32) (local $var2 i32) (local $var3 i32) (local $var4 i32) (local $var5 i32)
call $func18
...
(func $update (;24;) (export "update")
(local $var1 i32)
call $func18
...
(func $uninit (;25;) (export "uninit")
(local $var0 i32) (local $var1 i32) (local $var2 i32)
call $func18
...
我知道必须将构造函数称为 ,但是我如何控制它的位置并阻止每个函数调用它呢?是否可以通过clang ++的命令行参数来控制这一点?
这是我一直使用的命令:
clang++
-Wall
--target=wasm32
-Ofast
-flto
--no-standard-libraries
-Wl,--export=init
-Wl,--export=update
-Wl,--export=uninit
-Wl,--no-entry
-Wl,--lto-O3
-Wl,-allow-undefined-file=wasm.syms
-Wl,--import-memory
-o ./bin/main.wasm
-I./src
./src/lib.cpp
./src/main.cpp
我应该注意,我不使用emscripten或任何库/框架。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅
我认为正在发生的事情是在这种情况下,链接器决定您使用命令ABI,并且应用程序的每个输入点都是独立的命令。
您实际试图构建的是该文档中称为反应堆。
构建反应器的最简单方法是包括某种crt1.c文件,例如WASI-SDK使用的一个文件: https://github.com/webassembly/wasi-libc/blob/blob/main/libc-bottom-half/-half/crt/crt/crt1-reacector一下.c
如果导出
_Iniitialize
函数,又引用了__ wasm_call_ctors
函数,那是一个信号,这是链接器,您要称呼您的ctors。一次,您正在构建一个反应堆。See https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md
I think what is happening is that the linker in this case deciding that you using the command abi, and each entry point to your application is a standalone command.
What you are actually trying to build is that is referred to in that document as a reactor.
The easiest way to build a reactor is to include some kind of crt1.c file such as the one that wasi-sdk uses: https://github.com/WebAssembly/wasi-libc/blob/main/libc-bottom-half/crt/crt1-reactor.c
If you export the
_iniitialize
function, which in turn references the__wasm_call_ctors
function, that is a signal the linker that you the ctors are to be called just once and you are building a reactor.