# Export a thread-local variable as a weak symbol

**URL:** <https://ziggit.dev/t/export-a-thread-local-variable-as-a-weak-symbol/16149>\
**Category:** Help\
**Tags:** language, c, comptime\
**Created:** [June 13, 2026, 1:00pm UTC](https://ziggit.dev/t/export-a-thread-local-variable-as-a-weak-symbol/16149 "2026-06-13T13:00:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [June 13, 2026, 1:00pm UTC](https://ziggit.dev/t/export-a-thread-local-variable-as-a-weak-symbol/16149/1 "2026-06-13T13:00:16Z")

</div>

Hi!

I’m trying to export a thread-local variable as a weak symbol. In GNU C, this can be accomplished like so:

```c
#include <stdint.h>
__thread uint32_t__ attribute__((weak)) weak_threadlocal = 1;

```

```zig
$ zig build-obj weakthreadlocal.c
$ nm weakthreadlocal.o
0000000000000000 W weak_threadlocal

```

In Zig, `@export` would be the obvious choice:

```zig
threadlocal var weak_threadlocal: u32 = 1;
comptime {
    @export(&weak_threadlocal, .{
        .linkage = .weak,
        .name = "weak_threadlocal",
    });
}

```

But this can’t work, because pointers to thread-local variables cannot be comptime-known since they vary for each thread. And the obvious way of using `export` can’t work because it doesn’t support weak symbols. How can I resolve this?

---

<div class="post-metadata">

**Author:** ![g41797](https://ziggit.dev/user_avatar/ziggit.dev/g41797/32/3339_2.png) [@g41797](https://ziggit.dev/u/g41797)\
**Post date:** [June 13, 2026, 3:43pm UTC](https://ziggit.dev/t/export-a-thread-local-variable-as-a-weak-symbol/16149/2 "2026-06-13T15:43:28Z")

</div>

I am oof so can not check but if it hard achieve in zig possibly use result of C

for access to this var add to c helper function returning address of weak\_threadlocal

---

<div class="post-metadata">

**Author:** ![ali](https://ziggit.dev/user_avatar/ziggit.dev/ali/32/10235_2.png) [@ali](https://ziggit.dev/u/ali)\
**Post date:** [June 13, 2026, 5:49pm UTC](https://ziggit.dev/t/export-a-thread-local-variable-as-a-weak-symbol/16149/3 "2026-06-13T17:49:18Z")

</div>

I could not figure out a sane way to do this either. A workaround that also @g41797 mentioned, is declaring the variable in C and `extern`ing it in Zig.

```c
__thread int__ attribute__((weak)) weak_threadlocal = 1;

```

```zig
extern threadlocal var weak_threadlocal: c_int;

```

```zig
ali@nixos ~/d/zig (spirv)> zig build-obj weak.c weak.zig
ali@nixos ~/d/zig (spirv)> nm weak.o
0000000000000000 d builtin.output_mode
0000000000000000 W weak_threadlocal

```

Or you can patch it later via `objcopy --weaken-symbol=weak_threadlocal weak.o`. None are acceptable as a solution IMO so perhaps you should open an issue in [codeberg](https://codeberg.org/ziglang/zig/issues).
