将 C++ 程序与 SpiderMonkey 链接起来?

Linking a C++ program with SpiderMonkey?

我成功了compiled spidermonkey (on windows), how can I link against it now (to embed it)?

js-config is not properly installed, and I don't understand this workaround

链接到静态库应该更容易,但我什至不知道它是哪个文件。我在 dist/sdk/lib 中有 mozglue.lib, mozjs-43a1.lib, nspr4.lib, plc4.lib, plds4.lib,在 dist/lib 中有 nspr4.lib, plc4.lib, plds4.lib

更新

js-config 没有工作,因为我有 this problem-bash: '\r': command not found 因为 Windows/Unix 换行符问题,我 运行 dos2unix js-config 和我可以 运行 它)。

然而,输出没有帮助(windows):

$ ./js-config --cflags -std=gnu++0x -include /usr/local/include/mozjs-43a1/js/RequiredDefines.h -I/usr/local/include/mozjs-43a1 -Ic:/Users/Yvain/Documents/mozilla-central/js/src/build_OPT.OBJ/dist/include/nspr

$ ./js-config --libs ${libdir}/${JS_LIBRARY_NAME}.lib c:/Users/Yvain/Documents/mozilla-central/js/src/build_OPT.OBJ/dist/lib/nspr4.lib c:/Users/Yvain/Documents/mozilla-central/js/src/build_OPT.OBJ/dist/lib/plc4.lib c:/Users/Yvain/Documents/mozilla-central/js/src/build_OPT.OBJ/dist/lib/plds4.lib kernel32.lib user32.lib gdi32.lib winmm.lib wsock32.lib advapi32.lib psapi.lib

备注

我使用了如下命令编译:

g++ -std=c++11 -I<objdir>/dist/include -L<objdir>/dist/lib helloworld.cpp -o helloworld  -lmozjs-31 -lz -lpthread -ldl 

我知道这不是正确的编译方式,因为这些库不在 <objdir>/dist/lib 中。它returns以下错误:

[...]/jscpucfg.h:121:3: erreur:
#error "Cannot determine endianness of your platform. Please add support to jscpucfg.h."
[...]
erreur: ‘JS_EvaluateScript’ was not declared in this scope

这个问题似乎引起了一些注意。请注意,我问的是 same question for V8.

解决方法背后的想法是 运行 js-config --libs 并将结果放入 JSAPI_LD_FLAGS,可能会过滤掉 Darwin 上的内容,然后附加 JSAPI_LD_FLAGS到您的 LDFLAGS,这样您就可以 link 正确的库。

所以对于你的库问题,答案是构建 js-config,然后 运行 使用 --libs

同样,您可以使用您已经需要的 CFLAGS 和 js-config --cflags 的输出的组合来创建您的 CFLAGS。这是您可能已经发现自己在为其他库使用漂亮的 pkg-config 实用程序时所做的事情。

这不能解决字节序问题。你为什么不直接 运行 配置脚本?

在 Cygwin 项目中使用 spidermoney 的简单方法(您的一些输出表明您正在使用 Cygwin)是使用 Cygwin 可用的 libmozjs185 包。我的项目有点复杂,但 makefile 看起来像这样:

CFLAGS += -g -I/usr/include/js -DXP_UNIX
CXXFLAGS += -g -I/usr/include/js -DXP_UNIX
JSLINK=-lmozjs185

objs = <your .o files>

<your app>: $(objs)
    g++ -g -o <your app> $(objs) \
    $(JSLINK) \
    $(NULL)

如果您的应用只有 'C' 代码,请将上面的 g++ 更改为 gcc。除了 mozjs185 之外,我还有更多的库,我把它们放在 $(NULL) 之前。