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/go: import "C" breaks cross-compiling to Linux in OSX #19470

Closed
prasincs opened this issue Mar 9, 2017 · 1 comment
Closed

cmd/go: import "C" breaks cross-compiling to Linux in OSX #19470

prasincs opened this issue Mar 9, 2017 · 1 comment
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@prasincs
Copy link

prasincs commented Mar 9, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.8 darwin/amd64

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

OSX 10.12.3

Darwin Wintermute.local 16.4.0 Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64 i386 MacBookPro11,5 Darwin
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/pgautam/go"
GORACE=""
GOROOT="/Users/pgautam/go-src"
GOTOOLDIR="/Users/pgautam/go-src/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/sj/n5_cngpx27xd0cxrqry3j65h0000gn/T/go-build807902586=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

I created a file in a package proj with import "C" where C is never referenced in the code.

package proj

import "C"
import "fmt"

func Hello() {
        fmt.Println("Hello There!")
}

Then I made a main.go file with the following contents

-> % cat main.go
package main

import "github.com/prasincs/testproject/proj"

func main() {
        proj.Hello()
}

go build -o testproject . works as expected

but cross compiling fails

-> % GOOS=linux go build -o testproject  .
main.go:3:8: no buildable Go source files in /Users/pgautam/go/src/github.com/prasincs/testproject/proj

If I explicitly set CGO_ENABLED=1, I get an error at link time

-> % CGO_ENABLED=1 GOOS=linux go build -o testproject .
# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What did you expect to see?

I was expecting the following:

  • C is never used in the code but it is imported, that should be an error at build time
  • If the previous point is expected behavior, it should work with GOOS=linux enabled too.
  • CGO_ENABLED=1 should work if the issue is with cgo being turned off at cross-compile time.

What did you see instead?

I got the following instead of an error message regarding the spurious import "C" or a cross-compiled binary.

main.go:3:8: no buildable Go source files in /Users/pgautam/go/src/github.com/prasincs/testproject/proj
@ALTree ALTree changed the title import "C" breaks cross-compiling to Linux in OSX cmd/go: import "C" breaks cross-compiling to Linux in OSX Mar 9, 2017
@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 9, 2017
@robpike
Copy link
Contributor

robpike commented Mar 9, 2017

Cgo is not portable and cannot in general be used for cross-compilation as it requires the availability of C libraries on the target platform.

Sorry about this, but it's working as intended.

@robpike robpike closed this as completed Mar 9, 2017
@golang golang locked and limited conversation to collaborators Mar 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants