How to enforce static linking when running 'zig c++'?

I tried using zig c++ to build v8’s hello world demo app.
The output message shows that the linker treat the input static library files as TBD file, and the -v verbose message displays the underlying command is zig ld -dynamic.
How is it happen? And how could I enforce static linking?

❯ zig c++ -static -I. -Iinclude  -o v8_hello -fno-rtti -lv8_monolith \
-lv8_libbase -lv8_libplatform -ldl -Lout/arm64.release/obj -pthread \
-std=c++20 -DV8_COMPRESS_POINTERS \
-DV8_ENABLE_SANDBOX samples/hello-world.cc -v
zig ld -dynamic -platform_version macos 14.5.0 14.5 -syslibroot \
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk \
-rpath out/arm64.release/obj \
-rpath /nix/store/gd2wv2206pw1n6ddi2siy4y6vdrq2b6g-libcxx-11.1.0/lib \
-rpath /nix/store/alpyb4jjlfmajljzv567p31xa309axb6-libcxxabi-11.1.0/lib \
-rpath /nix/store/9krx1rq92zy5x5xr0dc17sydimkqicqw-compiler-rt-libc-11.1.0/lib \
-rpath /nix/store/cyz92bd5vaws21yj01vw0k695xkc58h7-libobjc-11.0.0/lib \
-rpath /nix/store/53z0h638fikqkij4916m2sjzpd1091bq-libcxx-18.1.8/lib \
-rpath /nix/store/kdycpy1zw6q0v28f2c3rwrfa7hl6gxx4-compiler-rt-libc-18.1.8/lib \
-rpath /nix/store/gd2wv2206pw1n6ddi2siy4y6vdrq2b6g-libcxx-11.1.0/lib \
-rpath /nix/store/alpyb4jjlfmajljzv567p31xa309axb6-libcxxabi-11.1.0/lib \
-rpath /nix/store/9krx1rq92zy5x5xr0dc17sydimkqicqw-compiler-rt-libc-11.1.0/lib \
-rpath /nix/store/cyz92bd5vaws21yj01vw0k695xkc58h7-libobjc-11.0.0/lib \
-rpath /nix/store/53z0h638fikqkij4916m2sjzpd1091bq-libcxx-18.1.8/lib \
-rpath /nix/store/kdycpy1zw6q0v28f2c3rwrfa7hl6gxx4-compiler-rt-libc-18.1.8/lib \
-e _main -o v8_hello \
out/arm64.release/obj/libv8_monolith.a \
out/arm64.release/obj/libv8_libbase.a out/arm64.release/obj/libv8_libplatform.a \
/Users/parents/.cache/zig/o/ff2597aca0da85e855ebb169e56b933e/hello-world.o \
-Lout/arm64.release/obj -L/nix/store/gd2wv2206pw1n6ddi2siy4y6vdrq2b6g-libcxx-11.1.0/lib \
-L/nix/store/alpyb4jjlfmajljzv567p31xa309axb6-libcxxabi-11.1.0/lib \
-L/nix/store/9krx1rq92zy5x5xr0dc17sydimkqicqw-compiler-rt-libc-11.1.0/lib \
-L/nix/store/cyz92bd5vaws21yj01vw0k695xkc58h7-libobjc-11.0.0/lib \
-L/nix/store/53z0h638fikqkij4916m2sjzpd1091bq-libcxx-18.1.8/lib \
-L/nix/store/kdycpy1zw6q0v28f2c3rwrfa7hl6gxx4-compiler-rt-libc-18.1.8/lib \
-L/nix/store/gd2wv2206pw1n6ddi2siy4y6vdrq2b6g-libcxx-11.1.0/lib \
-L/nix/store/alpyb4jjlfmajljzv567p31xa309axb6-libcxxabi-11.1.0/lib \
-L/nix/store/9krx1rq92zy5x5xr0dc17sydimkqicqw-compiler-rt-libc-11.1.0/lib \
-L/nix/store/cyz92bd5vaws21yj01vw0k695xkc58h7-libobjc-11.0.0/lib \
-L/nix/store/53z0h638fikqkij4916m2sjzpd1091bq-libcxx-18.1.8/lib \
-L/nix/store/kdycpy1zw6q0v28f2c3rwrfa7hl6gxx4-compiler-rt-libc-18.1.8/lib \
-F/nix/store/bd2l2sf1wh63cbc4g0ma30ryq0026kxz-apple-framework-CoreFoundation-11.0.0/Library/Frameworks \
-F /nix/store/bd2l2sf1wh63cbc4g0ma30ryq0026kxz-apple-framework-CoreFoundation-11.0.0/Library/Frameworks \
/Users/parents/.cache/zig/o/068df5d0c2d34e5357b4b7d1193e8068/libc++abi.a \
/Users/parents/.cache/zig/o/66eb39b1c33ce356f97460f5f1faa34d/libc++.a -lSystem \
/Users/parents/.cache/zig/o/21d8b40e342a8f3238bf92282522ec0f/libcompiler_rt.a \
error: failed to parse TBD file: UnexpectedToken
    note: while parsing out/arm64.release/obj/libv8_libplatform.a
error: failed to parse TBD file: UnexpectedToken
    note: while parsing out/arm64.release/obj/libv8_libbase.a

It is static linking almost everything (e.g. libcompiler_rt.a), but it cannot link statically to a library that have only a dynamic version (e.g. -lSystem).

Please see: Avoid Screenshots of Text/Code

The textual content has been appended.

But I still cannot understand why the errors generated. :pensive:

error: failed to parse TBD file: UnexpectedToken
    note: while parsing out/arm64.release/obj/libv8_libplatform.a
error: failed to parse TBD file: UnexpectedToken
    note: while parsing out/arm64.release/obj/libv8_libbase.a

It looks that these archives have inside corrupted TBD files.
Try to test the archives (tools/dev/gm.py arm64.release.check) and/or rebuild them.

Rebuild and running `tools/dev/gm.py check`, result is success.

TBD files only needed for dynamic linking.

libv8_monolithic.a, libv8_libbase.a, libv8_libplatform.a are all static library, and `nm` could list their symbols successfully.
(The only point of doubt is,
`file libv8_libbase.a` and `file libv8_libplatform.a` says “thin archive with xxx symbol entries”,
while `file libv8_monolithic.a` says “current ar archive”.)

But I suspect the root cause is the -dynamic argument in the transpiled command `zig ld -dynamic -platform_version macos ......`.

You can copy and run the command without the -dynamic to see what happens.


An idea is to run ranlib to create or update the index of the two archives.

兄弟是做嵌入式的吗 也对zig感兴趣?平时会用到这个吗

`zig ld` is invalid command.

I found out a way to transform the thin archive to current archive:
ar -T path/to/libv8_libbase.a | xargs ar rvs path/to/libv8_libbase.a.new
Now, the above TBD errors overcame.

But appeared heap of `undefined symbol`:

error: undefined symbol: \
__ZN2v88platform18NewDefaultPlatformEiNS0_\
15IdleTaskSupportENS0_21InProcessStackDumpingENSt3__\
110unique_ptrINS_17TracingControllerENS3_14default_deleteIS5\
_EEEENS0_12PriorityModeE

error: undefined symbol:\
 __ZNKSt4__Cr19__shared_weak_count13__get_deleterERKSt9type_info
error: undefined symbol:\
__ZNKSt4__Cr6locale9use_facetERNS0_2idE
error: undefined symbol: \
__ZNKSt4__Cr8ios_base6getlocEv
error: undefined symbol: \
__ZNSt4__Cr11__call_onceERVmPvPFvS2_E
error: undefined symbol: \
__ZNSt4__Cr12__get_sp_mutEPKv
error: undefined symbol: \
__ZNSt4__Cr12__next_primeEm
error: undefined symbol:\
__ZNSt4__Cr12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE17__assign_externalEPKc
error: undefined symbol: \
__ZNSt4__Cr12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE17__assign_externalEPKcm
......

Guess that stem from the library difference between `zig c++` and the compiler that built v8.

What compiler and settings were used to compile v8?

❯ ./third_party/llvm-build/Release+Asserts/bin/clang++ --version
clang version 19.0.0git (https://chromium.googlesource.com/a/external/\
github.com/llvm/llvm-project ecea8371ff03c15fb3dc27ee4108b98335fd2d63)
Target: arm64-apple-darwin23.5.0
Thread model: posix
InstalledDir: path/to/v8/v8/third_party/llvm-build/Release+Asserts/bin
❯ zig version
0.14.0-dev.719+f21928657
❯ zig c++ --version
Homebrew clang version 18.1.8
Target: arm64-apple-darwin23.5.0
Thread model: posix
InstalledDir: .devbox/nix/profile/default/bin