Why This Document Exists

Yesterday, I attempted to build the source code for v8, primarily to obtain the associated libraries, whether dynamic or static. The goal was to use these libraries to understand interoperability between C++ and JavaScript, specifically how JavaScript can call C++ functions and access properties, and how C++ can call JavaScript functions and access properties. To achieve this, it is necessary to obtain the relevant v8 libraries. About two years ago, I also tried debugging v8, but that time I referred to IDE-related documentation and used VS Code to run the helloworld example. That attempt involved compiling directly from the source code and using a debugger, which is quite different from this current attempt. This time, my attempt to build v8 encountered many inexplicable issues. These problems are not explicitly explained in the documentation, or perhaps the authors assumed everyone would know them. However, that is not the case; the documentation is simply poorly written. As a result, following the documentation does not lead to a smooth build of the desired artifacts. I am unsure of the specific situations on other platforms, but in my own environment, I indeed encountered many problems. For example, the generated static library could not be linked and was identified as an unknown architecture. Later, I realized that I should use lld for linking, and the issue was resolved. However, there were also issues with custom C++ libraries, which prevented the successful linking of the static library. After repeatedly trying and modifying relevant build parameters, I finally obtained the expected static library by not using clang and excluding the custom C++ libraries. These details were also not mentioned in the documentation and should actually be explained, especially the implicit relationships between build parameters. Therefore, I believe it is necessary to write a summary and explanation of these issues for reference by others who may encounter similar problems.

Environment

uname -a
# Linux shubuzuo-system 6.8.0-52-generic #53-Ubuntu SMP PREEMPT_DYNAMIC Sat Jan 11 00:06:25 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
cat /etc/issue
# Ubuntu 24.04.1 LTS \n \l
clang -v
# Ubuntu clang version 18.1.3 (1ubuntu1)
# Target: x86_64-pc-linux-gnu
# Thread model: posix
# InstalledDir: /usr/bin
# Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/13
# Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/13
# Candidate multilib: .;@m64
# Candidate multilib: 32;@m32
# Candidate multilib: x32;@mx32
# Selected multilib: .;@m64
gcc -v
# Using built-in specs.
# COLLECT_GCC=gcc
# COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-linux-gnu/13/lto-wrapper
# OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
# OFFLOAD_TARGET_DEFAULT=1
# Target: x86_64-linux-gnu
# Configured with: ../src/configure -v --with-pkgversion='Ubuntu 13.3.0-6ubuntu2~24.04' --with-bugurl=file:///usr/share/doc/gcc-13/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-13 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-13-fG75Ri/gcc-13-13.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-13-fG75Ri/gcc-13-13.3.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
# Thread model: posix
# Supported LTO compression algorithms: zlib zstd
# gcc version 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04) 

Initial Trial and Error

Building V8 from source

Checking out the V8 source code

Building V8 with GN

The following lists the relevant main commands. For details, please refer to the reference documents above.

# depot_tools 工具
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
export PATH=/path/to/depot_tools:$PATH
mkdir ~/v8
cd ~/v8
# 拉代码
fetch v8
cd v8
# 拉相关代码及依赖库
git pull && gclient sync
# 查看最近打的一个 tag, 一般情况下就是最新发布的版本
git tag --sort=-creatordate | head -n 1
# 13.5.119 # 预期笔者当前最新的发布版本
# build 会默认生成一份 args.gn 该文件用于后续 build 相关参数配置, build 工具为 ninja
tools/dev/gm.py x64.release

The args.gn file generated by default in my environment is as follows:

# 执行该语句将会自动生成,默认位置为 out/x64.release/args.gn
tools/dev/gm.py x64.release
# # mkdir -p out/x64.release
# # echo > out/x64.release/args.gn << EOF
# is_component_build = false
# is_debug = false
# target_cpu = "x64"
# v8_enable_sandbox = true
# v8_enable_backtrace = true
# v8_enable_disassembler = true
# v8_enable_object_print = true
# v8_enable_verify_heap = true
# dcheck_always_on = false
# EOF
# # gn gen out/x64.release
is_component_build = false
is_debug = false
target_cpu = "x64"
v8_enable_sandbox = true
v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
dcheck_always_on = false
# 默认编译出来的相关库
cd out/x64.release/obj
ls *.a # 静态库 因为 is_component_build = false
# libv8_libbase.a  libv8_libplatform.a

When using it, especially during local testing, you might want to reference a single library instead of importing individual modules. In such cases, all artifacts need to be bundled into a single static library. However, there are times when you want to import dynamic libraries one by one. How can this be achieved?

Setting is_component_build to true generates dynamic libraries for several modules. After making changes, you must regenerate the build-related files.

is_component_build = true
is_debug = false
target_cpu = "x64"
v8_enable_sandbox = true
v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
dcheck_always_on = false
cd out/x64.release
# gn 参考 https://v8.dev/docs/build-gn
gn gen .
# 重新 build
tools/dev/gm.py x64.release

For generating a single static library, the change is as follows:

is_component_build = false
is_debug = false
target_cpu = "x64"
v8_enable_sandbox = true
v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
dcheck_always_on = false
v8_monolithic = true
v8_use_external_startup_data = false
cd out/x64.release
# gn 参考 https://v8.dev/docs/build-gn
gn gen .
# 重新 build
ninja -C out/x64.release v8_monolith

The libraries built using the above command have several issues. First, during linking, there is an unknown architecture problem with the libraries. Second, because v8 uses a custom C++ standard library, the linker cannot recognize standard C++ and v8 functions during the linking process. Although v8 functions are indeed present in the library, this ultimately results in a linking failure.

Static Library Unknown Architecture

When building using v8_monolith or v8_libbase with v8_libplatform, and using the hello world example code, the following error occurred: unknown architecture of input file /tmp/v8/v8/out/x64.release/obj/libv8_monolith.a(api.o) is incompatible with i386:x86-64 output. The screenshot is shown below:

unknown architecture of input file

Later, it was discovered that the execution statement in the documentation Getting started with embedding V8 is:

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

Suspecting the issue might be related to linking, using lld for linking avoids this problem.

Customizing the C++ Standard Library

After resolving the aforementioned issue, other problems may still arise. A screenshot is provided below:

Undefined reference to v8platformNewDefaultPlatform

This issue was encountered by others a few years ago. See the related discussion at Undefined reference to v8platformNewDefaultPlatform.

Subsequent Attempts

target_cpu = "x64"
target_os = "linux"
is_debug = true
is_component_build = false
v8_monolithic = true
v8_static_library = false
v8_use_external_startup_data = false
use_custom_libcxx = false
is_clang = false
cd out/x64.release
# gn 参考 https://v8.dev/docs/build-gn
gn gen .
# 重新 build
ninja -C out/x64.release v8_monolith

This is fine. hello world can be compiled and linked, and runs and debugs correctly (debugging within the hello world program).

Reflection

The causes of the aforementioned issues, as well as their resolution, largely stem from the documentation. Why? If the documentation had clearly specified the linker used for the build, the occurrence of the first issue could have been avoided. The second issue arose entirely due to a lack of relevant reference information. Providing detailed documentation for args.gn related to v8 is highly necessary. Although the expected output was eventually achieved, this process was achieved through trial and error. In contrast, if more detailed information had been provided, it would have served as a useful reference. For v8 maintainers, resolving this issue should be straightforward, as subsequent coding and debugging all depend on this fundamental step. Regarding the resolution of the Undefined reference to v8platformNewDefaultPlatform issue, it was ultimately resolved when the original poster experimented with the appropriate configuration. However, given this exchange—especially considering that the necessity of documenting v8 args.gn was already highlighted years ago—it appears that the v8 team either did not recognize this issue or did not implement any related improvements. This serves as a cautionary reminder for us to be careful when writing documentation. Documentation should act as a guiding light, leading users in the right direction, rather than adding to the darkness. Otherwise, it will lead to greater confusion for users or force them to spend significant time and effort searching for answers.