gocross-compilingcgozig

Zig as C Linux->Mac cross-compiler for go project with go-sqlite3 error: unable to find dynamic system library 'resolv'


I'm building a Go application with native (CGO_ENABLED=1) SQLite support using https://github.com/mattn/go-sqlite3.

I'm trying to get the builder docker image to a reasonable size. (goreleaser/goreleaser-cross works for all platforms, but is over 8GB).

When using Zig (0.14.0) as the cross compiler, Linux and Windows targets build/run fine on both an Alpine and Debian/bookworm-image (go 1.24.1) but mac compilation fails with:

/usr/local/go/pkg/tool/linux_amd64/link: running zig failed: exit status 1
/opt/zig/zig cc -target x86_64-macos -arch x86_64 -m64 -Wl,-headerpad,1144 -o $WORK/b001/exe/a.out /tmp/go-link-2504977538/go.o /tmp/go-link-2504977538/000000.o /tmp/go-link-2504977538/000001.o /tmp/go-link-2504977538/000002.o /tmp/go-link-2504977538/000003.o /tmp/go-link-2504977538/000004.o /tmp/go-link-2504977538/000005.o /tmp/go-link-2504977538/000006.o /tmp/go-link-2504977538/000007.o /tmp/go-link-2504977538/000008.o /tmp/go-link-2504977538/000009.o /tmp/go-link-2504977538/000010.o /tmp/go-link-2504977538/000011.o /tmp/go-link-2504977538/000012.o /tmp/go-link-2504977538/000013.o /tmp/go-link-2504977538/000014.o /tmp/go-link-2504977538/000015.o /tmp/go-link-2504977538/000016.o /tmp/go-link-2504977538/000017.o /tmp/go-link-2504977538/000018.o /tmp/go-link-2504977538/000019.o /tmp/go-link-2504977538/000020.o /tmp/go-link-2504977538/000021.o /tmp/go-link-2504977538/000022.o -lresolv -O2 -g -O2 -g -lpthread -framework CoreFoundation -framework Security
error: unable to find dynamic system library 'resolv' using strategy 'paths_first'. searched paths: none

What I understand is the resolv-library is supposed to be part of libc, so if Zig doesn't provide it (by not having a musl-implementation for Mac), it should come from a Mac SDK.

I've also tried supplying it with:

(export GOOS=darwin && export GOARCH=amd64 && export CC="zig cc -target x86_64-macos --sysroot ${MACOS_SDK} -isysroot ${MACOS_SDK} -I${MACOS_SDK}/usr/include -Wno-nullability-completeness" && go build -o dist/$GOOS/$GOARCH/)

To no avail, still getting the "unable to find dynamic system library"...

What might be missing? How can I get CGO Linux→Mac cross-compilation working with a recent Zig and Go?


Solution

  • Ultimately, I ended up retaining Zig for Linux/Windows and using an Osxcross-image for Mac, resulting in a ~2.5GB image:

    Dockerfile

    FROM crazymax/osxcross:14.5-r0-alpine as osxcross
    FROM golang:1.24-alpine
    
    RUN apk update
    RUN apk add curl zip gettext clang lld musl-dev
    
    RUN go install github.com/jstemmer/go-junit-report/v2@latest
    
    RUN curl https://ziglang.org/download/0.14.0/zig-linux-x86_64-0.14.0.tar.xz | tar x -J -C /opt
    RUN ln -s /opt/zig* /opt/zig
    
    ENV PATH="$PATH:/opt/zig"
    
    RUN mkdir /.cache
    RUN chmod -R 777 /.cache
    RUN chmod -R 777 /go/pkg/mod
    
    COPY --from=osxcross /osxcross /osxcross
    
    ENV PATH="/osxcross/bin:$PATH"
    ENV LD_LIBRARY_PATH="/osxcross/lib:$LD_LIBRARY_PATH"
    

    Jenkinsfile excerpt

    sh '''#!/bin/sh
        export CGO_ENABLED=1
        
        #https://github.com/goreleaser/goreleaser-cross?tab=readme-ov-file#supported-toolchainsplatforms
        (export GOOS=darwin  && export GOARCH=amd64 && export CC="o64-clang"   && go build -o dist/$GOOS/$GOARCH/)
        (export GOOS=darwin  && export GOARCH=arm64 && export CC="oa64-clang"  && go build -o dist/$GOOS/$GOARCH/)
        #https://ziglang.org/download/0.14.0/release-notes.html#Support-Table
        (export GOOS=linux   && export GOARCH=amd64 && export CC="zig cc -target x86_64-linux"   && go build -o dist/$GOOS/$GOARCH/)
        (export GOOS=windows && export GOARCH=amd64 && export CC="zig cc -target x86_64-windows" && go build -o dist/$GOOS/$GOARCH/)
    
        go test -coverprofile=coverage.out -v 2>&1 ./... | go-junit-report -set-exit-code > report.xml
    '''
    junit testResults: 'report.xml'