--- title: Link -lsocket -lnsl for socket(), connect() and getaddrinfo() area: link impact: HIGH tags: [link, libsocket, libnsl, configure, autoconf, gcc15, go, porting] verified: 2026-11-04 --- # Link -lsocket -lnsl for socket(), connect() and getaddrinfo() illumos keeps `socket()`, `connect()`, `getaddrinfo()`, `freeaddrinfo()` and `gai_strerror()` in libsocket and `gethostbyname()` in libnsl, not in libc. Link with `-lsocket -lnsl`, or the link editor stops with "symbol referencing errors". A project that drops old-Solaris support can lose this check; gcc 15 did, and broke its own build. **Antipattern** ```sh cc -o client client.c # Linux habit: no network library on the link line # Undefined first referenced # symbol in file # ld: fatal: symbol referencing errors. No output written to client # gcc 15 removed NETLIBS (-lsocket -lnsl) with its Solaris < 11.4 support; its cc1plus link then failed: # undefined __xnet_connect, __xnet_socket, __xnet_getaddrinfo, freeaddrinfo, gai_strerror ``` **The Solnix way** ```sh cc -o client client.c -lsocket -lnsl # the SYNOPSIS of socket(3SOCKET), connect(3SOCKET), getaddrinfo(3SOCKET) # in a recipe whose build does not find them (progress.md x86-wave5: mosquitto needed +lsocket +lnsl for # gai_strerror/gethostby*): ./configure --prefix="$out" LIBS="-lsocket -lnsl" # tmux (aa-xbuild7) added -lresolv too; a per-program _LDADD # can still miss them (one inetutils wave needed a cc shim) # to fix it upstream, put the check in configure.ac, as gcc 15's restored check does: # AX_LIB_SOCKET_NSL (from gcc's config/ax_lib_socket_nsl.m4) elfdump -d "$out/bin/client" | grep libsocket # a NEEDED entry for libsocket.so.1 ``` 0002 edits gcc's own build files (c++tools, the cc1plus link), not the link specs: packages still need the flags. `pkgs.solnix.gcc15` carries OpenIndiana's gcc-15 patches 0001-0039, 0002 among them. Go binaries are dynamic on illumos: they NEED libc.so, libsocket.so and libsendfile.so, and RUNPATH names illumos-userland. **The illumos way** (OpenIndiana, OmniOS, SmartOS) ```sh cc example.c -lnsl -lsocket # connect(3SOCKET) Example 1 cc example.c -lxnet # the X/Open variant: socket(3XNET), libxnet(3LIB) ``` OpenIndiana's oi-userland gcc-15 patch 0002 is the fix Solnix took verbatim. OmniOS's omniosorg/gcc il-15_2_0 branch has the same content. **The difference.** Same as illumos: the same libsocket.so.1 and libnsl.so.1, but Solnix bakes /nix/store paths into RUNPATH. One build-platform link (native openssl, aarch64 cross wave) failed on `-lnsl` with MD5Init and mp_* undefined: the seed's libnsl.so needs libmd.so.1 and libmp.so.2, and no `-L` reached bare links for them. The fix added `-lmd -lmp` and the extraSyslibs `-L`, which the x86 native stdenv already had. **Check.** `elfdump -d | grep -E 'libsocket|libnsl'` shows which of the two the binary NEEDs: libsocket for a program that calls `socket()` or `getaddrinfo()`, and libnsl where it was linked and is used (the stdenv's ld wrapper keeps `-z ignore`, which drops an unused one; ld's own default is `-z record`). Where configure has the check, `config.log` shows `checking for library containing socket`. **Unverified** - SmartOS: the sources do not say what its gcc does. **Sources** - [ld(1)](https://solnix.io/docs/man/ld.1) (illumos, CDDL), -z ignore | -z record (record is the default) - [docs/design-solaris-stdenv.md](https://codeberg.org/solnix/solnix/src/commit/5a8e5ce75a33086d7be4410149847bdf7501b8fc/docs/design-solaris-stdenv.md) (solnix 5a8e5ce), §2.1: linking through ld-solaris-wrapper.sh "keeps `-z ignore`" - [libsocket(3LIB)](https://solnix.io/docs/man/libsocket.3lib) (illumos, CDDL), SYNOPSIS (`-lsocket`); INTERFACES (socket, connect, getaddrinfo, freeaddrinfo, gai_strerror, __xnet_connect, __xnet_socket) - [libnsl(3LIB)](https://solnix.io/docs/man/libnsl.3lib) (illumos, CDDL), SYNOPSIS (`-lnsl`); INTERFACES (gethostbyname) - [socket(3SOCKET)](https://solnix.io/docs/man/socket.3socket) (illumos, CDDL), SYNOPSIS "cc [ flag ... ] file ... -lsocket -lnsl [ library ... ]" - [connect(3SOCKET)](https://solnix.io/docs/man/connect.3socket) (illumos, CDDL), SYNOPSIS; EXAMPLES, Example 1 (`cc example.c -lnsl -lsocket`) - [getaddrinfo(3SOCKET)](https://solnix.io/docs/man/getaddrinfo.3socket) (illumos, CDDL), SYNOPSIS - [libxnet(3LIB)](https://solnix.io/docs/man/libxnet.3lib) (illumos, CDDL), SYNOPSIS (`-lxnet`) - [socket(3XNET)](https://solnix.io/docs/man/socket.3xnet) (illumos, CDDL), SYNOPSIS - [elfdump(1)](https://solnix.io/docs/man/elfdump.1) (illumos, CDDL), -d (the .dynamic section) - [docs/aarch64-native-readiness-verdict.md](https://codeberg.org/solnix/solnix/src/commit/5a8e5ce75a33086d7be4410149847bdf7501b8fc/docs/aarch64-native-readiness-verdict.md) (solnix 5a8e5ce), `elfdump -d bin/openssl` -> NEEDED libsocket.so.1, libnsl.so.1 - [handoff: 20261102-toolchain-trials-newer-versions](https://codeberg.org/solnix/solnix-infra/src/commit/c2ecf4535e0fc32261bbfb2414bc7961a73fa6ca/docs/handoff/20261102-toolchain-trials-newer-versions/README.md), gcc 15: the five fixes, item 2 (cc1plus link, OpenIndiana's 0002 verbatim) - [handoff: gcc.patch](https://codeberg.org/solnix/solnix-infra/src/commit/c2ecf4535e0fc32261bbfb2414bc7961a73fa6ca/docs/handoff/20261102-toolchain-trials-newer-versions/gcc.patch), 0002-Restore-linking-against-lsocket-lnsl.patch (header; diffstat (Makefile.in, configure, Make-lang.in); configure.ac `AX_LIB_SOCKET_NSL`, `NETLIBS`; the generated `checking for library containing` checks) - [docs/gcc15.md](https://codeberg.org/solnix/solnix/src/commit/5a8e5ce75a33086d7be4410149847bdf7501b8fc/docs/gcc15.md) (solnix 5a8e5ce), intro (`pkgs.solnix.gcc15`; stdenv is gcc 14); The patches (0001-0039 from OpenIndiana; same as omniosorg/gcc il-15_2_0) - [handoff: from-builder-20261028T233541-cache7-stage2-go-rows-request](https://codeberg.org/solnix/solnix-infra/src/commit/c2ecf4535e0fc32261bbfb2414bc7961a73fa6ca/docs/handoff/from-builder-20261028T233541-cache7-stage2-go-rows-request.md), What we ask (Go binaries NEED libc.so, libsocket.so, libsendfile.so) - [docs/progress.md](https://codeberg.org/solnix/solnix/src/commit/5a8e5ce75a33086d7be4410149847bdf7501b8fc/docs/progress.md) (solnix 5a8e5ce), x86-wave5 (mosquitto: "illumos socket funcs (gai_strerror/gethostby*) in libsocket+libnsl"); aa-xbuild7 (tmux `LIBS="-lresolv -lsocket -lnsl"`); haproxy-3.0.6 (Solaris ld "symbol referencing errors", fixed by adding -lnsl -lsocket -lrt); the SPARCv9 wave 10 inetutils entry ("LIBS= only reaches LDADD (per-program _LDADD overrides it"; "cc shim that appends -lsocket -lnsl on every link") - [docs/design-pkgsrc-patches.md](https://codeberg.org/solnix/solnix/src/commit/5a8e5ce75a33086d7be4410149847bdf7501b8fc/docs/design-pkgsrc-patches.md) (solnix 5a8e5ce), §3 The recurring illumos-fix classes (`-lnsl` / `-lsocket` / `-lresolv`) - [.kiro/steering/illumos-domain.md](https://codeberg.org/solnix/solnix/src/commit/5a8e5ce75a33086d7be4410149847bdf7501b8fc/.kiro/steering/illumos-domain.md) (solnix 5a8e5ce), libc / linker / runtime (libc split into libc, libsocket, libnsl, libresolv; /nix/store paths in RUNPATH) - [docs/aarch64-native-openssl-fix.md](https://codeberg.org/solnix/solnix/src/commit/5a8e5ce75a33086d7be4410149847bdf7501b8fc/docs/aarch64-native-openssl-fix.md) (solnix 5a8e5ce), Exact failure ("symbol referencing errors"); Root cause (libnsl.so needs libmd.so.1, libmp.so.2); The fix (extraSyslibs `-L`)