SpiderMonkey local compile: collect2: error: ld returned 1 exit status

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.

New contributor

IAmStupidSoBePatient is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật