Javascript 在JS中,我应该在哪里定义emscripten外部函数? 假设在C++中定义为函数代码>: extern "C" void x();

Javascript 在JS中,我应该在哪里定义emscripten外部函数? 假设在C++中定义为函数代码>: extern "C" void x();,javascript,emscripten,asm.js,Javascript,Emscripten,Asm.js,我在全局上下文中用JS实现它 function _x() { console.log('x called'); } \u x是在asm编译的js文件中定义的,该文件将被调用,而不是我的实现。我做错了什么 我在链接时收到以下警告: warning: unresolved symbol: x 以下是stacktrace: Uncaught abort() at Error at jsStackTrace (http://localhost/module.js:978:13) at stackTr

我在全局上下文中用JS实现它

function _x() { console.log('x called'); }
\u x
是在asm编译的js文件中定义的,该文件将被调用,而不是我的实现。我做错了什么

我在链接时收到以下警告:

warning: unresolved symbol: x
以下是stacktrace:

Uncaught abort() at Error
at jsStackTrace (http://localhost/module.js:978:13)
at stackTrace (http://localhost/module.js:995:22)
at abort (http://localhost/module.js:71106:25)
at _x (http://localhost/module.js:5829:46)
at Array._x__wrapper (http://localhost/module.js:68595:41)
at Object.dynCall_vi (http://localhost/module.js:68442:36)
at invoke_vi (http://localhost/module.js:7017:25)
at _LoadFile (http://localhost/module.js:7573:6)
at asm._LoadFile (http://localhost/module.js:69219:25)
at eval (eval at cwrap (http://localhost/module.js:554:17), <anonymous>:6:26)
Uncaught abort()出错
在jsStackTrace(http://localhost/module.js:978:13)
在stackTrace(http://localhost/module.js:995:22)
中止(http://localhost/module.js:71106:25)
在(http://localhost/module.js:5829:46)
在数组中(http://localhost/module.js:68595:41)
在Object.dynCall_vi(http://localhost/module.js:68442:36)
第六次会议(http://localhost/module.js:7017:25)
at_LoadFile(http://localhost/module.js:7573:6)
在asm.\u加载文件(http://localhost/module.js:69219:25)
在评估时(在cwrap评估时)(http://localhost/module.js:554:17), :6:26)

如果你想把一个字符串从C++传递到JavaScript,你不必走这个路线。相反,您可以使用直接内联Javascript。然后可以调用您定义的Javascript函数,并传递它们的值

此外,要传递字符串,您需要确保传递C样式的字符串,然后对结果使用Emscripten提供的
Pointer\u stringify
Javascript函数:

#include <string>

#include <emscripten.h>

int main()
{
  std::string myString("This is a string in C++");

  EM_ASM_ARGS({
    console.log(Pointer_stringify($0));
  }, myString.c_str());

  return 0;
}
#包括
#包括
int main()
{
std::string myString(“这是C++中的字符串”);
EM_ASM_ARGS({
log(指针字符串化($0));
},myString.c_str());
返回0;
}
如中所述,您可以通过使用一个Javascript文件来定义库,该文件调用
mergeInto
,以将对象与Javascript函数合并到
LibraryManager.library
。然后使用
--js library
选项进行编译,以传入库的位置

例如,可以将Javascript文件与单个函数库一起使用

// In library.js
mergeInto(LibraryManager.library, {
  x: function() {
    alert('hi');
  },
});
调用这个函数

的主C++文件
// in librarytest.cc
extern "C" void x();

int main()
{
  x();
  return 0;
}
并使用

em++ librarytest.cc --js-library library.js -o librarytest.html

然后你在浏览器中加载
librarytest.html
,它会显示一个带有
hi

的警告框。你能澄清你的目标/用例是什么吗?emscripten编译的JS到普通JS之间的通信()在emscripten编译的JS到普通JS之间传递字符串()是否可以用“在Javascript中实现C API”来完成?因为这更具可读性。