I am using Go 1.6 on OSX 10.11.4 and trying to cross compile my go program for windows and linux. I use a library (https://github.com/pebbe/zmq4) in my go program which is a Go binding for a C based implementation. I followed the instructions to cross compile for x86 windows as listed here by issuing the following command:
env GOOS=windows GOARCH=386 go build -v znode.go
However the above command gives me the following error
znode.go:15:2: C source files not allowed when not using cgo or SWIG: dummy.c
Is there a workaround for this? I saw other posts on stackoverflow which suggest moving to go1.5 but i am already on go1.6
FWIW after six years ... user720694's comment has the clue. If there are any .c or .cpp/cc/etc. files in the build directory, adding CGO_ENABLED=0 before go build does the trick.
Related
When I try to cross compile my golang project from OSX to Linux, then I get following error message:
# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed
and the compilation aborts.
This is how I try to build my application:
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build
I also tried using gox:
gox -os="linux"
but it still did not work.
Everything works as expected if I do not use the GOOS=linux tag and I am able to build/run my project for/on my OSX machine successfully.
I can confirm that the command
$env CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -v main.go
works perfectly fine with a "Hello World" main.go file on MacOS X High Sierra without installing anything else than just go (see also here).
As already pointed out in the comments, you are probably trying to compile with cgo and obviously lack parts of the tool chain and/or headers and that is why your linker throws an error. Please provide an acceptable example, otherwise we won't be able to help you.
You need to install a proper toolchain to do that.
In order to build binaries for architectures different that your build
host, you need far more than just a cross-compiler - you need a
full-blown toolchain, which can be a real pain to create, as you
probably discovered.
A couple of approaches:
Use a proper Linux distribution in a virtual machine, such as
VirtualBox. If you only want to build binaries for Linux/i386 on an
MacOSX/x86_64 host, this is - in my opinion - the easiest, safest and
most clean solution. It is not a cross-compiler, of course, but it
works and it has the added advantage that you can actually test your
executables.
Use a script such crosstool-NG (a descendant of the original
crosstool) to automatically build the toolchain - definitely easier
than building it on your own, although you may have to compromise for
slightly older compiler versions.
Cross compiler for linux on mac os x
I am attempting to compile a go program. The code is a small x11 window manager, found HERE. (The code is 98 lines, too long to post here)
Here is the command I run, on x86-64 Ubuntu:
GOOS=linux GOARCH=386 go build littlewm.go
This command works fine on other files in the same directory. However, when I try to compile this one, I get the following error:
can't load package: package main: no buildable Go source files in /home/ubuntu/workspace/Go
This does not involve the gopath variable, as I have successfully compiled other programs in the same directory. I have a suspicion it involves the code itself or some option I am lacking. As such, this is not really a duplicate of an existing question and is quite possibly unique, since I am not getting the answer I want from other similar questions. Any help is appreciated greatly.
Thanks!
I encountered a similar problem when trying to build my own go program.
I had just added GCO to interface with some native C code in a library.
It built for me for the native platform (the machine I was coding on), but not when I tried to target another platform (using GOOS and GOARCH as you're doing).
Setting CGO_ENABLED to 1 (as mentioned in david's comment) fixed the problem for me:
CGO_ENABLED=1 GOOS=linux GOARCH=386 go build littlewm.go
I was using a makefile to build for multiple platforms using multiple rules, so for me, I put
export CGO_ENABLED = 1
near the top of my makefile so that I didn't need to specify it for each platform.
I git clone code from github.com/google/gxui
and then
cd samples/hello_word
GOOS=windows go build
an error occurred
it says
/d01/gopath/src/github.com/goxjs/gl/gl_opengl.go:10:2: no buildable Go
source files in /d01/gopath/src/github.com/go-gl/gl/v2.1/gl
/d01/gopath/src/github.com/goxjs/glfw/desktop.go:10:2: no buildable Go
source files in /d01/gopath/src/github.com/go-gl/glfw/v3.2/glfw
I try to
go get -u github.com/go-gl/gl/v2.1/gl go get -u
github.com/go-gl/glfw/v3.2/glfw
no error occurred
I try to run again
GOOS=windows go build
The problem has not been solved
This could be happening because of your mac doesn't have a c compiler that can cross compiler to the target OS. I tried it on a Linux OS and it compiles without any issues. there was an issue on git about it last year and it's still open till today. I would suggest compiling it directly on the target OS. If you look throught the code in
github.com/go-gl/gl/v2.1/gl/conversions.go
you will notice that it import "c" meaing it will require a C compiler that is compartible with the target OS.
I recently started trying go to program some web based applications. At first, everything went fine, until I wanted to cross compile a binary for a different platform. I'm running MacOS and I wanted to compile a binary for linux, so I changed GOOS to linux and GOARCH to amd64. Since then, I always get the error message
go tool: no such tool "compile"
I'm using GoClipse, but running the compile manually by
go install hello.go
I get the same error. When changing back to compiling for darwin architecture, I get the same error now, so basically I'm totally unable to compile any code written in Go at the moment.
I installed it via the binary packages provided by google. In the end I actually got it back to work by just reinstalling it. Sometimes I'm just blind to the easy solution.
After that, I succeeded in cross compiling for linux machine after compiling the necessary cross-compilers by running env GOOS=linux GOARCH=arm GOROOT_BOOTSTRAP=/usr/local/go ./make.bash --no-clean from the Go sources directory.
Thanks for all you efforts, sorry to have kept you busy on such a simple matter.
You have the wrong x64 or x386 package installed most likely. I had 32 installed running 64 bit. Reinstall fixed. Good luck.
You might be able to get away with that if you install Go 1.5, however if you use anything that depends on cgo, you will have to install a cross-compiler linker or install Linux on a virtual machine to be able to cross compile for Linux.
In my case is due to the fact that GOPATH and GOROOT are not set correctly, maybe you can check go env. Here is a discussion may be useful.
The way I figured-out what file is read and caused Go to look for the compile binary in the wrong place can be solved using:
$ strace go tool -n compile 2>&1 |grep openat
openat(AT_FDCWD, "/home/nwaizer/.config/go/env", O_RDONLY|O_CLOEXEC) = 3
In that file, the IDE Goland, add a path to some project, causing the havoc.
I am currently working on Mac OS X, now I need to build a .exe file so that the Go program can run on windows.
So here is the question, how to build a executable file for Win X86 under MacOS amd64 ? or Is it impossible to do so ?
If you don't use CGo but pure Go, then it's perfectly doable and standard.
First you have to make the Go environment on your development computer for the targets. Supposing your Go installation is in ~/var/go, this may be this :
cd ~/var/go/src
CGO_ENABLED=0 GOOS=windows GOARCH=386 ./make.bash
Then you compile with the good GOOS and GOARCH :
GOOS=windows GOARCH=386 go build -o hello.exe hello.go
Here's the go-wiki page on building a windows exe on linux.
I've used Dave Cheney's tutorial and accompanying shell scripts to build binaries for linux, windows and OS X on linux just fine. (Used by my stressdisk project)
It is exactly the same method as described by dystroy just with step by step instructions and a few helpful shell aliases.
Update Feb 2021
If you want to plug something into your CI then I recommend goreleaser - this can automatically cross compile for a variety of OSes and build packages too. If you want to see an example check out the stressdisk releases page built entirely with goreleaser - see the config and the instructions