嵌入 SpiderMonkey JS

Embedding SpiderMonkey JS

我正在开发 C++ 应用程序。我想在应用程序中嵌入 SpiderMonkey。

我正在使用 CMake,但无法构建。因此,为了减少并发症,我尝试了此 page 上的示例代码。这不会 link 从命令行使用 cmake 或 gcc。

所以,更简单的是,为了确保我可以 link 正确,我正在尝试使以下内容正常工作。 从带有 gcc 的命令行:

g++ --std=c++11 
  -I/home/thetasinner/moz/js/src/build_DBG.OBJ/dist/include     
  -L/home/thetasinner/moz/js/src/build_DBG.OBJ/js/src -DDEBUG 
  -Wl,--verbose -lmozjs-54a1 -lm -lz -ldl test.cpp -o test

在以下最小代码示例中:

#include <iostream>
#include <stdexcept>
#include "jsapi.h"
#include "js/Initialization.h"

int main(int argc, char** args) {
  if (!JS_Init()) {
    throw std::runtime_error("failed to initialise.");  
  }

  std::cout << "It's alive!\n";

  JS_ShutDown();
  return 0;
}

即使这样 link。我收到错误

/tmp/ccqjx5RY.o: In function `main':
  custom.cpp:(.text+0xf2): undefined reference to `JS_ShutDown()'
/tmp/ccqjx5RY.o: In function `JS_Init()':
  custom.cpp:(.text._Z7JS_Initv[_Z7JS_Initv]+0xa): undefined reference to
      'JS::detail::InitWithFailureDiagnostic(bool)'
collect2: error: ld returned 1 exit status

已找到 headers,linker 正在查找 mozjs 库

attempt to open /home/thetasinner/moz/js/src/custom_build_DBG.OBJ/js/src 
  /libmozjs-54a1.so succeeded
-lmozjs-54a1 (/home/thetasinner/moz/js/src/custom_build_DBG.OBJ/js/src
  /libmozjs-54a1.so)

我正在研究 Linux(Ubuntu 16.04 和 Debian 8.7 已尝试)因为构建工具就在那里。我什至不想碰 Window。

在 spidermonkey 构建中构建的 'js' 可执行文件工作正常,我假设其中包含我正在尝试 link 的库。所以我会认为 lib 本身构建得很好。

谁能帮我解决这些 linker 错误?关于旧版本的 SpiderMonkey 的问题有很多答案,但没有关于较新版本的答案。我对 45 版(我试过但出现非常相似的错误)或 52 版感兴趣。 我很乐意在代码中挖掘如何在它构建后用它做我想做的事情,因此对没有正确记录的最新版本感兴趣,我完全被构建步骤难住了。

我怀疑这只是命令行上的排序问题:

g++ --std=c++11 
-I/home/thetasinner/moz/js/src/build_DBG.OBJ/dist/include     
-L/home/thetasinner/moz/js/src/build_DBG.OBJ/js/src -DDEBUG 
test.cpp -o test
-Wl,--verbose -lmozjs-54a1 -lm -lz -ldl 

先编译,后链接,库按依赖顺序排列。 (我的第一个猜测是您忽略了在命令行中提及 mozjs。再次查看发现它只是在错误的位置。)