You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

构建WebAssembly项目时出现emval相关未定义符号错误求助

问题描述

我尝试构建一个可使用JS对象引用(类型为emscripten::val或emscripten::EM_VAL)的简单WebAssembly应用,代码如下:

#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/val.h>

extern "C"
{
    EMSCRIPTEN_KEEPALIVE void Draw(emscripten::val canvas)
    {
        emscripten::val::global("console").call<void>("log", canvas);
    }
}

我分别用emcc、Emscripten CMake工具链以及Blazor WebAssembly(C#/.NET)编译这段代码,但无论哪种方式,都出现相同的未定义符号错误:

NativeClient_0.o: undefined symbol: _emval_get_global
NativeClient_0.o: undefined symbol: _emval_decref
NativeClient_0.o: undefined symbol: _emval_call_void_method
NativeClient_0.o: undefined symbol: _emval_get_method_caller
NativeClient_0.o: undefined symbol: _emval_incref

我查过相关问题但没找到有效解决方法,请问哪里操作有误?

解决方法

这些未定义符号属于Emscripten的embind模块(用于C++与JS互操作),你的编译流程未正确链接该模块,不同编译方式的修复方案如下:

1. 使用emcc直接编译

编译时必须添加--bind参数,它会自动引入embind相关实现代码:

emcc your_code.cpp --bind -o output.js

2. 使用Emscripten CMake工具链

在CMakeLists.txt中,需明确链接emscripten::embind目标:

add_executable(your_app your_code.cpp)
target_link_libraries(your_app PRIVATE emscripten::embind)
# 若需导出函数,可补充设置:
set_target_properties(your_app PROPERTIES
    LINK_FLAGS "-s EXPORTED_FUNCTIONS=['_Draw'] -s EXPORTED_RUNTIME_METHODS=['ccall','cwrap']"
)

3. Blazor WebAssembly(C#/.NET)场景

在原生项目配置中,确保编译时传入--bind参数。比如在NativeMethods.txt对应的编译指令中添加该参数,或者在项目MSBuild配置里设置:

<EmccExtraArgs>--bind</EmccExtraArgs>

核心原因:emscripten::val是embind模块提供的API,默认编译不会包含该模块实现,必须通过--bind参数或链接embind目标显式引入这些符号。

内容的提问来源于stack exchange,提问作者D-Kay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:23:24