On 26 May 2016 at 23:09, Ain <ain.v...@gmail.com> wrote:
> Hi
>
> I would like to set up go tools in a way that allows cross-compilation even
> when c code is used. Ie I'd like to use "github.com/mattn/go-sqlite3"
> package (and others which depend on c) in a project which I want to cross
> compile for windows and linux. Is this is possible and if yes then how?
In principle you just need to point CC at the cross compiler when
building and set CGO_ENABLED=1, e.g.
CGO_ENABLED=1 CC=powerpc64le-linux-gnu-gcc GOARCH=ppc64 go build -o
go.ppc64 cmd/go
I've only done cross-arch compiling as above, not cross-OS as you are
talking about. mingw seems to be packaged for 16.04 but I don't know
exactly which package you need. You may also have some fun installing
the development headers for sqlite in a way that works for the cross
compiler, not sure.
Take a look at goxc. It's been created just for this.
You want GOARCH, not GOARC. The latter will be ignored and the default will match your current CPU.
Can I convince you to abandon the idea of cross compilation _with_ cgo. Cross compilation without cgo is trivial, you get it for free with every Go install, but _with_ cgo requires you to have a complete working C compiler for the target environment and a precompiled C library to link against. This might be static or dynamic, usually whichever is the least convenient.Compare that to building in a VM, which is super simple, requires no oddball cross compilation C compilers, and is super well tested.
Please consider it, you'll have a better and less frustrating experience.
reede, 27. mai 2016 12:57.14 UTC+3 kirjutas Dave Cheney:You want GOARCH, not GOARC. The latter will be ignored and the default will match your current CPU.(blush)Fixed that and win32 version compiled too.Can I convince you to abandon the idea of cross compilation _with_ cgo. Cross compilation without cgo is trivial, you get it for free with every Go install, but _with_ cgo requires you to have a complete working C compiler for the target environment and a precompiled C library to link against. This might be static or dynamic, usually whichever is the least convenient.Compare that to building in a VM, which is super simple, requires no oddball cross compilation C compilers, and is super well tested.Do I understand right, that you suggest me to set up win32, win64, linux32 etc VMs with go installed and then build my project on each of them, to get binary for corresponding platform?That looks like a lot of VMs to manage... unless I only need one win and one linux setup (IOW can go build win64 exe on win32 enviromnent without cgo?).
Please consider it, you'll have a better and less frustrating experience."less frustrating experience" is exactly what I'm looking for. However I thought that managing (at least) four VMs with go and thirdparty libs is going to be frustrating, that's why I started looking for how to produce all target binaries from single setup.
On Fri, 27 May 2016 04:22:38 -0700 (PDT)
Ain <ain.v...@gmail.com> wrote:
[...]
> > Compare that to building in a VM, which is super simple, requires
> > no oddball cross compilation C compilers, and is super well tested.
> Do I understand right, that you suggest me to set up win32, win64,
> linux32 etc VMs with go installed and then build my project on each
> of them, to get binary for corresponding platform?
> That looks like a lot of VMs to manage... unless I only need one win
> and one linux setup (IOW can go build win64 exe on win32 enviromnent
> without cgo?).
[...]
Without cgo, Go on any platform is able to build for any other
combination of GOOS+GOARCH.
The real problem is that once you have cross-compiled, you will need to
actually test the compiled executable.
Unfortunately there is still lot of useful stuff which is not available in pure go.
ain