Skip to content

cmd/go: problems with cgo_resnew when cross-compiling on freebsd for linux #66839

@ov

Description

@ov

Go version

go version go1.22.1 freebsd/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/ov/.cache/go-build'
GOENV='/home/ov/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='freebsd'
GOINSECURE=''
GOMODCACHE='/home/ov/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='freebsd'
GOPATH='/home/ov/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go122'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go122/pkg/tool/freebsd_amd64'
GOVCS=''
GOVERSION='go1.22.1'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='cc'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/home/ov/...../go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2801792256=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I'm trying to cross-compile a project for Linux on a FreeBSD host:

GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC="gcc13" CXX="g++13" go build -ldflags "-extldflags -static" -o app

I tried different CC/CXX options (clang, gcc, amd64-gcc) - they all behave the same.

What did you see happen?

Here's what I get:

GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC="gcc13" CXX="g++13" go build -ldflags "-extldflags -static" -o app
# net
/usr/local/go122/src/net/cgo_resnew.go:20:76: cannot use _Ctype_socklen_t(len(b)) (value of type _Ctype_uint) as _Ctype_ulong value in argument to (_C2func_getnameinfo)

There was a similar discussion about this in #33101 and the solution was to set CGO_ENABLED=0. The problem is that my project has some c++ code that needs to be built, so I can't disable cgo.

If I manually modify the flags in cgo_resnew.go and cgo_resold.go so the latter is used for Linux, the compiling progresses, but it then fails on linking with:

/usr/local/go122/pkg/tool/freebsd_amd64/link: running g++13 failed: exit status 1
/usr/local/bin/ld: cannot find -lresolv: No such file or directory
collect2: error: ld returned 1 exit status

I am not sure if modifying the resnew/old was the right thing, so decided to not dig any deeper and create the report instead.

What did you expect to see?

I expect to be able to do cross-compile builds that use the net module with CGO_ENABLED=1. I can do this on Mac with musl, I can do normal builds on FreeBSD. I expect the cross-builds work on FreeBSD, as well.

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions