gpt4 book ai didi

javascript - SpiderMonkey 编译错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:45:51 26 4
gpt4 key购买 nike

我刚刚安装了最新版本的 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,
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++ 编译时):

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'

谁能告诉我我做错了什么?

最佳答案

可能你在编译时没有给出包含路径和库路径。

你应该这样编译:

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 是您的文件名。

关于javascript - SpiderMonkey 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26022848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com