Javascript SpiderMonkey编译错误
我刚刚安装了最新版本的SpiderMonkey,正在尝试运行示例“Hello World”程序来嵌入JS引擎。示例程序如下所示:Javascript SpiderMonkey编译错误,javascript,c++,spidermonkey,Javascript,C++,Spidermonkey,我刚刚安装了最新版本的SpiderMonkey,正在尝试运行示例“Hello World”程序来嵌入JS引擎。示例程序如下所示: #include "jsapi.h" /* The class of the global object. */ static JSClass global_class = { "global", JSCLASS_GLOBAL_FLAGS, JS_PropertyStub, JS_DeletePropertyStub,
#include "jsapi.h"
/* The class of the global object. */
static JSClass global_class = {
"global",
JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub,
JS_DeletePropertyStub,
JS_PropertyStub,
JS_StrictPropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub
};
int main(int argc, const char *argv[])
{
JS_Init();
JSRuntime *rt = JS_NewRuntime(8L * 1024 * 1024, JS_NO_HELPER_THREADS);
if (!rt)
return 1;
JSContext *cx = JS_NewContext(rt, 8192);
if (!cx)
return 1;
JS::RootedObject global(cx, JS_NewGlobalObject(cx, &global_class, nullptr, JS::DontFireOnNewGlobalHook));
if (!global)
return 1;
JS::Value rval;
bool ok;
{
JSAutoCompartment ac(cx, *global);
JS_InitStandardClasses(cx, *global);
const char *script = "'hello'+'world, it is '+new Date()";
const char *filename = "noname";
int lineno = 0;
ok = JS_EvaluateScript(cx, *global, script, strlen(script), filename, lineno, &rval);
if (rval.isNull() | rval.isFalse() )
return 1;
}
JSString *str = rval.toString();
printf("%s\n", JS_EncodeString(cx, str));
JS_DestroyContext(cx);
JS_DestroyRuntime(rt);
JS_ShutDown();
return 0;
}
但是,我得到了以下错误(当我在终端上使用g++编译时):
有人能告诉我我做错了什么吗?可能您在编译时没有给出include路径和library路径 您应该以这种方式编译: sudo g++-std=c++11 -I/home/pi/mozjs-24.2.0/js/src/build_OPT.OBJ/dist/include-L/home/pi/mozjs-24.2.0/js/src/build_OPT.OBJ/dist/lib/HelloWorld.cpp-o HelloWorld-lmozjs-24-lz-lpthread-ldl 其中
HelloWorld
是您的文件名
js/src/helloworld.cpp:21:53: error: use of undeclared identifier 'JS_NO_HELPER_THREADS'
js/src/helloworld.cpp:36:25: error: no matching constructor for initialization of 'JSAutoCompartment'
js/src/helloworld.cpp:37:7: error: no matching function for call to 'JS_InitStandardClasses'
js/src/helloworld.cpp:42:12: error: no matching function for call to 'JS_EvaluateScript'