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

**URL:** <https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454>\
**Category:** Help\
**Tags:** build-system\
**Created:** [August 1, 2024, 1:09pm UTC](https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454 "2024-08-01T13:09:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![i11010520](https://ziggit.dev/user_avatar/ziggit.dev/i11010520/32/268_2.png) [@i11010520](https://ziggit.dev/u/i11010520)\
**Post date:** [August 1, 2024, 1:09pm UTC](https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454/1 "2024-08-01T13:09:20Z")

</div>

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
❯ 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
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

```

 ![截屏2024-08-01 20.54.23](https://ziggit.dev/uploads/default/original/2X/e/e40000b0df492fba62e95b1878b9684b3ab7e577.jpeg)

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [August 1, 2024, 1:30pm UTC](https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454/2 "2024-08-01T13:30:51Z")

</div>

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](https://ziggit.dev/t/avoid-screenshots-of-text-code/5369)

---

<div class="post-metadata">

**Author:** ![i11010520](https://ziggit.dev/user_avatar/ziggit.dev/i11010520/32/268_2.png) [@i11010520](https://ziggit.dev/u/i11010520)\
**Post date:** [August 1, 2024, 1:58pm UTC](https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454/3 "2024-08-01T13:58:39Z")

</div>

The textual content has been appended.

But I still cannot understand why the errors generated. 😔

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [August 1, 2024, 4:45pm UTC](https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454/4 "2024-08-01T16:45:46Z")

</div>

```zig
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.

---

<div class="post-metadata">

**Author:** ![i11010520](https://ziggit.dev/user_avatar/ziggit.dev/i11010520/32/268_2.png) [@i11010520](https://ziggit.dev/u/i11010520)\
**Post date:** [August 2, 2024, 7:21am UTC](https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454/5 "2024-08-02T07:21:47Z")

</div>

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

> [@dimdin](#):
>
> It looks that these archives have inside corrupted TBD files.

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 ......``.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [August 2, 2024, 7:41am UTC](https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454/6 "2024-08-02T07:41:23Z")

</div>

> [@i11010520](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![unisgn](https://ziggit.dev/user_avatar/ziggit.dev/unisgn/32/3105_2.png) [@unisgn](https://ziggit.dev/u/unisgn)\
**Post date:** [August 2, 2024, 9:48am UTC](https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454/7 "2024-08-02T09:48:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![i11010520](https://ziggit.dev/user_avatar/ziggit.dev/i11010520/32/268_2.png) [@i11010520](https://ziggit.dev/u/i11010520)\
**Post date:** [August 2, 2024, 10:22am UTC](https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454/8 "2024-08-02T10:22:24Z")

</div>

> [@dimdin](#):
>
> You can copy and run the command without the `-dynamic` to see what happens.

``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``:

```zig
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`.

---

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [August 2, 2024, 4:09pm UTC](https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454/9 "2024-08-02T16:09:34Z")

</div>

What compiler and settings were used to compile `v8`?

---

<div class="post-metadata">

**Author:** ![i11010520](https://ziggit.dev/user_avatar/ziggit.dev/i11010520/32/268_2.png) [@i11010520](https://ziggit.dev/u/i11010520)\
**Post date:** [August 2, 2024, 4:48pm UTC](https://ziggit.dev/t/how-to-enforce-static-linking-when-running-zig-c/5454/10 "2024-08-02T16:48:40Z")

</div>

```zig
❯ ./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
❯ zig version
0.14.0-dev.719+f21928657

```

```zig
❯ zig c++ --version
Homebrew clang version 18.1.8
Target: arm64-apple-darwin23.5.0
Thread model: posix
InstalledDir: .devbox/nix/profile/default/bin

```
