zig icon indicating copy to clipboard operation
zig copied to clipboard

[zig cc] libresolv symbol not found under linux

Open michaelortmann opened this issue 1 month ago • 4 comments

Zig Version

0.15.2

Steps to Reproduce and Observed Behavior

> cat test.c 
#include <resolv.h>
#include <stdio.h>
#include <string.h>

int main() {
    const char *s = "test";
    char out[sizeof s << 1];

    b64_ntop((const unsigned char *) s, strlen(s), out, sizeof out);

    printf("%s\n", out);
}
> clang -Wall test.c -o test -lresolv`
> ./test
dGVzdA==
> zig cc -Wall test.c -o test -lresolv
ld.lld: error: undefined symbol: __b64_ntop
>>> referenced by test.c:9
>>>               /home/michael/.cache/zig/o/d3eb4eae11967bfaa9bf667ce900d74b/test.o:(main)

Under FreeBSD it is working fine without -lresolv

Expected Behavior

I expect zig cc to resolve symbols (in system libc/libresolv/zigs musl libc)

michaelortmann avatar Nov 02 '25 05:11 michaelortmann

(and yes, the sizeof s i taking the size of the pointer, but it doesnt matter for this example)

michaelortmann avatar Nov 02 '25 05:11 michaelortmann

What kind of system are you on? You haven't passed a cross -target flag, so it'll be a native build.

alexrp avatar Nov 02 '25 11:11 alexrp

Arch Linux (glibc 2.42). I am not trying to cross compile. What i meant was, when i try to compile it native under FreeBSD it works, but when i try to compile it native under Linux (glibc) it doesnt.

michaelortmann avatar Nov 03 '25 04:11 michaelortmann

The problem is that isLibCLibName considers resolv to be a libc library name:

https://github.com/ziglang/zig/blob/a6d444c2714f24b7232895cf15282e2287fe445e/lib/std/zig/target.zig#L342-L355

So it gets removed from the linker inputs on the assumption that it will be specially handled by the compiler:

https://github.com/ziglang/zig/blob/a6d444c2714f24b7232895cf15282e2287fe445e/src/main.zig#L3832-L3839

And it does indeed get handled when cross-compiling:

https://github.com/ziglang/zig/blob/a6d444c2714f24b7232895cf15282e2287fe445e/src/libs/glibc.zig#L37-L47 https://github.com/ziglang/zig/blob/a6d444c2714f24b7232895cf15282e2287fe445e/src/link/Lld.zig#L1210-L1221

But not for a native build:

https://github.com/ziglang/zig/blob/a6d444c2714f24b7232895cf15282e2287fe445e/src/target.zig#L432-L435 https://github.com/ziglang/zig/blob/a6d444c2714f24b7232895cf15282e2287fe445e/src/link/Lld.zig#L1205-L1209

I'll need to do some thinking about how to handle this. The easy fix is of course just to add -lresolv to libcFullLinkFlags, but maybe we need to rethink this stuff.

alexrp avatar Nov 05 '25 05:11 alexrp