[zig cc] libresolv symbol not found under linux
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)
(and yes, the sizeof s i taking the size of the pointer, but it doesnt matter for this example)
What kind of system are you on? You haven't passed a cross -target flag, so it'll be a native build.
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.
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.