I am trying to compile this code:
#include <cstdio>
#include "requirements/jsapi.h"
#include "requirements/js/CompilationAndEvaluation.h"
#include "requirements/js/SourceText.h"
#include "requirements/boilerplate.h"
static bool ExecuteCodePrintResult(JSContext* cx, const char* code) {
JS::CompileOptions options(cx);
options.setFileAndLine("noname", 1);
JS::SourceText<mozilla::Utf8Unit> source;
if (!source.init(cx, code, strlen(code), JS::SourceOwnership::Borrowed)) {
return false;
}
JS::RootedValue rval(cx);
if (!JS::Evaluate(cx, options, source, &rval)) return false;
// There are many ways to display an arbitrary value as a result. In this
// case, we know that the value is an ASCII string because of the expression
// that we executed, so we can just print the string directly.
printf("%sn", JS_EncodeStringToASCII(cx, rval.toString()).get());
return true;
}
static bool HelloExample(JSContext* cx) {
JS::RootedObject global(cx, boilerplate::CreateGlobal(cx));
if (!global) {
return false;
}
JSAutoRealm ar(cx, global);
// The 'js' delimiter is meaningless, but it's useful for marking C++ raw
// strings semantically.
return ExecuteCodePrintResult(cx, R"js(
`hello world, it is ${new Date()}`
)js");
}
int main(int argc, const char* argv[]) {
if (!boilerplate::RunExample(HelloExample)) {
return 1;
}
return 0;
}
Files compiled from “requirements/” are the same as the ones that the install puts in /usr/local. My goal is to get an executable, but instead I get the following in the command line:
In file included from /usr/include/time.h:29,
from /usr/include/pthread.h:23,
from /usr/include/x86_64-linux-gnu/c++/12/bits/gthr-default.h:35,
from /usr/include/x86_64-linux-gnu/c++/12/bits/gthr.h:148,
from /usr/include/c++/12/ext/atomicity.h:35,
from /usr/include/c++/12/bits/ios_base.h:39,
from /usr/include/c++/12/ios:42,
from /usr/include/c++/12/ostream:38,
from requirements/mozilla/Maybe.h:13,
from requirements/jsapi.h:14,
from test.cpp:3:
requirements/js/HeapAPI.h:263:47: warning: ‘offsetof’ within non-standard-layout type ‘js::gc::TenuredChunkBase’ is conditionally-supported [-Winvalid-offsetof]
263 | const size_t ChunkMarkBitmapOffset = offsetof(TenuredChunkBase, markBits);
| ^
/usr/bin/ld: /tmp/ccCGKPu2.o: in function `ExecuteCodePrintResult(JSContext*, char const*)':
test.cpp:(.text+0xfe): undefined reference to `JS::CompileOptions::CompileOptions(JSContext*)'
/usr/bin/ld: test.cpp:(.text+0x1ce): undefined reference to `JS::Evaluate(JSContext*, JS::ReadOnlyCompileOptions const&, JS::SourceText<mozilla::Utf8Unit>&, JS::MutableHandle<JS::Value>)'
/usr/bin/ld: test.cpp:(.text+0x207): undefined reference to `JS_EncodeStringToASCII(JSContext*, JSString*)'
/usr/bin/ld: /tmp/ccCGKPu2.o: in function `HelloExample(JSContext*)':
test.cpp:(.text+0x2eb): undefined reference to `boilerplate::CreateGlobal(JSContext*)'
/usr/bin/ld: test.cpp:(.text+0x348): undefined reference to `JSAutoRealm::JSAutoRealm(JSContext*, JSObject*)'
/usr/bin/ld: test.cpp:(.text+0x36d): undefined reference to `JSAutoRealm::~JSAutoRealm()'
/usr/bin/ld: test.cpp:(.text+0x39f): undefined reference to `JSAutoRealm::~JSAutoRealm()'
/usr/bin/ld: /tmp/ccCGKPu2.o: in function `main':
test.cpp:(.text+0x3f1): undefined reference to `boilerplate::RunExample(bool (*)(JSContext*), bool)'
/usr/bin/ld: /tmp/ccCGKPu2.o: in function `bool JS::SourceText<mozilla::Utf8Unit>::initImpl<JSContext>(JSContext*, mozilla::Utf8Unit const*, unsigned long, JS::SourceOwnership)':
test.cpp:(.text._ZN2JS10SourceTextIN7mozilla8Utf8UnitEE8initImplI9JSContextEEbPT_PKS2_mNS_15SourceOwnershipE[_ZN2JS10SourceTextIN7mozilla8Utf8UnitEE8initImplI9JSContextEEbPT_PKS2_mNS_15SourceOwnershipE]+0xd1): undefined reference to `JS::detail::ReportSourceTooLong(JSContext*)'
/usr/bin/ld: /tmp/ccCGKPu2.o: in function `mozilla::Array<js::StackRootedBase*, 15ul>::operator[](unsigned long)':
test.cpp:(.text._ZN7mozilla5ArrayIPN2js15StackRootedBaseELm15EEixEm[_ZN7mozilla5ArrayIPN2js15StackRootedBaseELm15EEixEm]+0x31): undefined reference to `mozilla::detail::InvalidArrayIndex_CRASH(unsigned long, unsigned long)'
collect2: error: ld returned 1 exit status
The command I use is g++ test.cpp -o test -Wall -DDEBUG=1
and it gives some warnings (see above), and I should be getting an executable called test.
The problem isn’t obvious to me, and most of the resources online tell me nothing for my situation. The code I got is directly from “spidermonkey-embedding-examples” except I didn’t want to use the library in /usr/local, for a variety of stupid reasons I have to work with on my team.
IAmStupidSoBePatient is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.