golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.02k stars 17.54k forks source link

x/mobile: Undefined symbols _res_9_nclose with Go 1.20 #58416

Open xDragonZ opened 1 year ago

xDragonZ commented 1 year ago

What version of Go are you using (go version)?

$ go version
go version go1.20 darwin/arm64

Does this issue reproduce with the latest release?

Using MacOS with Go 1.20 error, no issue in 1.19.5

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/name/Library/Caches/go-build"
GOENV="/Users/name/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/name/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/name/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/hr/1lxcf_8s41dfqq139phd6twm0000gn/T/go-build1419606280=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

After compiling using gomobile bind -v -ldflags='-s -w' -target=ios -o libtest.xcframework github.com/example/test in terminal and import the libtest.xcframework library to xcode, it will show error in xcode when trying to compile ios app

What did you expect to see?

What did you see instead?

Undefined symbols for architecture arm64: "_res_9_nclose", referenced from: _internal/syscall/unix.libresolv_res_9_nclose_trampoline.abi0 in Libtest(go.o) "_res_9_ninit", referenced from: _internal/syscall/unix.libresolv_res_9_ninit_trampoline.abi0 in Libtest(go.o) "_res_9_nsearch", referenced from: _internal/syscall/unix.libresolv_res_9_nsearch_trampoline.abi0 in Libtest(go.o) ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Related: https://github.com/golang/go/issues/58159

bcmills commented 1 year ago

(attn @golang/ios)

bcmills commented 1 year ago

import the library to xcode

Import how? (Is there something you need to configure in xcode to set the linker flags for the final executable?)

xDragonZ commented 1 year ago

Import xcframework to xcode project target. No, nothing to configure in xcode

bcmills commented 1 year ago

@xDragonZ, try adding libresolv.9.tbd and/or libresolv.tbd in the Linked Frameworks and Libraries section. (See https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-06-rust-on-ios.html.)

xDragonZ commented 1 year ago

Thank you 🫡 finally got it working.

Passing extldflags=-lresolv to gomobile doesn't works but adding libresolv.tbd able to compile the ios app without error now.

bcmills commented 1 year ago

Great! Want to send a CL to update the documentation? (Probably goes in one of the files in https://cs.opensource.google/go/x/mobile/+/master:cmd/gomobile/ ..?)

gurupras commented 1 year ago

Could you please share how you got this working? Running into the same issue and somehow adding libresolv.tbd and libresolv.9.tbd (for me it shows under Apple SDK and not IOS SDK; not sure if that matters) does nothing.

xDragonZ commented 1 year ago

@gurupras you need to add it to "Frameworks & Libraries" section too

drew-512 commented 1 year ago

Hi friends,

Not clear if downstream now has to deal with this from now on, or if this will be addressed upstream (making many gophers happy).

tmm1 commented 1 year ago

Adding to Frameworks & Libraries wasn't working for me. I had to go to Build Phases > Link Libraries and add it there

yulianaboglione commented 1 year ago

Hola puedes dejar lo Pasos que hiciste plis??? Tengo el mismo problema y no entiendo bien lo qué hay que hacer por favor necesito saber qué fue lo que hizo

dreacot commented 8 months ago

include -tags=netgo while compiling e.g gomobile bind -v -tags=netgo -ldflags='-s -w' -target=ios -o libtest.xcframework

notatestuser commented 5 months ago

Unfortunately, the suggested fix to use -tags=netgo appears to cause DNS resolution issues on real iOS devices running iOS 17+. It seems that Apple is preventing DNS resolvers embedded by apps to work properly.