Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: failed to run c-archive library built for iOS #41177

Open
rogchap opened this issue Sep 2, 2020 · 7 comments
Open

cmd/cgo: failed to run c-archive library built for iOS #41177

rogchap opened this issue Sep 2, 2020 · 7 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@rogchap
Copy link

rogchap commented Sep 2, 2020

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

$ go version
go version go1.15.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/roger/Library/Caches/go-build"
GOENV="/Users/roger/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/roger/go/pkg/mod"
GONOPROXY="github.com/XXXXXX"
GONOSUMDB="github.com/XXXXXX"
GOOS="darwin"
GOPATH="/Users/roger/go"
GOPRIVATE="github.com/XXXXXXX"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/roger/Development/XXXXX/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/y8/8fn4c6b54y38lyvqcmsxk9hc0000gn/T/go-build298028240=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Create a simple go program that has C export function:

package main

import "C"

//export sayHello
func sayHello() string {
	return "Hello from Go\x00"
}

func main() {}

build the code with the following:

CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 CC=$(shell go env GOROOT)/misc/ios/clangwrap.sh CGO_CFLAGS="-fembed-bitcode" go build -buildmode=c-archive -tags ios -o arm64.a .

Build is successful.

Add the arm64.a and arm64.h to an iOS project in XCode, and add #import "arm64.h" to the bridging header file.

Call the sayHello function from Swift:

let hello = sayHello()
self.helloLabel = String.init(cString: hello.p, encoding: .utf8)!

What did you expect to see?

XCode to build successfully, and the helloLabel to be updated with "Hello from Go".

What did you see instead?

I get a very strange error from XCode that states iOS-arm64 != iOS-arm64!:

ignoring file /Users/roger/Development/XXXXX/arm64.a, building for iOS-arm64 but attempting to link with file built for iOS-arm64
$ lipo -info arm64.a
Non-fat file: arm64.a is architecture: arm64

Ps. I previously had this working successfully, but I can't see what changed other than upgrading to Go 1.15 (I tried downgrading to 1.14.6 but no change) and updating Catalina to 10.15.6

@cherrymui
Copy link
Member

(I tried downgrading to 1.14.6 but no change)

Could you clarify what is "no change"? It still doesn't work after downgrading, or it goes back to work? Thanks.

That error message is indeed mysterious...

@rogchap
Copy link
Author

rogchap commented Sep 2, 2020

Does not work after downgrade; same error with 1.14.6

@dmitshur dmitshur added mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Sep 3, 2020
@dmitshur dmitshur added this to the Backlog milestone Sep 3, 2020
@rogchap
Copy link
Author

rogchap commented Sep 14, 2020

Seems to be an environment issue that I have yet to track down. I've been able to take the same code and build successfully on another machine; copy the library to the failing machine and now XCode builds successfully.

@daxzhuo
Copy link

daxzhuo commented Aug 15, 2021

I encountered the same problem but the build mode is "archive", it work with build mode "c-archive", can someone take a look in this?

package main

import "C"

//export getInt
func getInt() C.int {
    return 10
}
go version go1.16.4 darwin/amd64
env CGO_ENABLED=1 GOOS=ios GOARCH=arm64 CC=(go env GOROOT)/misc/ios/clangwrap.sh go build -v -buildmode=archive  -o libgo.a .

@cherrymui
Copy link
Member

@daxzhuo build mode "archive" is intended to build a Go package, not a C library. See go help buildmode for more detail. Thanks.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
@skywalker-nick
Copy link

I got the same error. Here is my environment:

Golang: v1.20.6
Compiler: Apple clang version 15.0.0 (clang-1500.0.40.1)
iOS Target Version: Minimal 10.0
iPhoneOS SDK Version: 17.0
Xcode Version: 14.3.1

@rogchap Hi, you mentioned it worked in another environment. Could you share the differences?

@rogchap
Copy link
Author

rogchap commented Oct 8, 2023

I wasn't able to track down the exact issue. The two machines had the same versions of everything; one succeeded, one had this error.

The only thing I was less confident on was XCode and xcode-tools, which may have had a rouge setting 🤷‍♂️

Unlikely the solution you want, but I ended up rebuilding my machine, installing everything from scratch and it fixed the issue. 😬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Status: Triage Backlog
Development

No branches or pull requests

6 participants