macosv8clang++

Clang++, ld: Undefined Symbols while running V8 sample application in MacOS 14.3


I am trying to run sample V8 embedded application referred from here: [https://v8.dev/docs/embed][1] . (hello-world.cc)

I have build the v8 from source, and could locate all the libraries such as libv8_monolith, libv8_libbase, libv8_libplatform.

When I execute command

clang++ -I. -Iinclude samples/hello-world.cc -o hello_world -fno-rtti -lv8_monolith -lv8_libbase -lv8_libplatform -ldl -Lout.gn/x64.release.sample/obj/ -pthread -std=c++17 -DV8_COMPRESS_POINTERS -DV8_ENABLE_SANDBOX

I got below error while linking. It could not find the symbols.

ld: Undefined symbols:   v8::platform::NewDefaultPlatform(int, v8::platform::IdleTaskSupport, v8::platform::InProcessStackDumping, std::__1::unique_ptr<v8::TracingController, std::__1::default_delete<v8::TracingController>>, v8::platform::PriorityMode), referenced from:
      _main in hello-world-c8e5eb.o   std::__Cr::__shared_weak_count::__get_deleter(std::type_info const&) const, referenced from:
      vtable for std::__Cr::__shared_ptr_pointer<v8::internal::BackingStore*, std::__Cr::default_delete<v8::internal::BackingStore>, std::__Cr::allocator<v8::internal::BackingStore>> in libv8_monolith.a[57](api.o) ...... ...... 

[More lines]

clang: error: linker command failed with exit code 1

OS: macOS Sonoma 14.3


Solution

  • By default, V8 links against its own bundled copy of libc++. That's what you get by following the convenience instructions at v8.dev/docs/build. Building V8 against libc++ requires you to link your embedding application against (a matching version of) libc++ as well, which is totally doable but might be inconvenient, especially if you have other dependencies and constraints.

    To make embedding easier, follow the instructions at v8.dev/docs/embed, including the build instructions there. The v8.gen.py step listed there produces a build configuration that disables usage of the bundled libc++, linking against your system's C++ library instead (probably libstdc++, on most systems).
    Of course, the listed steps just offer convenience, and you can also make the required changes yourself: run gn args out/x64.release, and in the editor that opens, add use_custom_libcxx = false. Save and quit (to cause regeneration of build files), then recompile V8. Then linking against it should Just Work™ (at least as far as the C++ standard library is concerned).