Search code examples
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).


    Side note: on SO or really anywhere, when asking questions, it helps to be specific and precise.

    Post the exact command(s) you ran. Don't copy-paste a command line from one documented workflow when you've in fact been following a different workflow.
    (In this case: the -Lout.gn/x64.release.sample/obj/ part and the beginning of your question looks like you followed v8.dev/docs/embed, which should have given you the build config you need; but the error message and your later comment clarified that you actually followed v8.dev/docs/build, where that argument would have been -Lout/x64.release/obj/.)

    When someone asks for the contents of your args.gn file, post the contents of that file, instead of running some perhaps-vaguely-related command and reporting on partial observations you made there. When someone volunteers to help you, give them what they ask for in order to be able to help you.

    Ideally, post complete repro instructions, including any code that's required to reproduce your issue, and every single command that needs to be run. If it's more than you want to fit into the question, upload relevant files e.g. as a GitHub gist or project. Don't make people guess what you may have done or meant.