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

Cross platform build failed #106

Closed
elgs opened this issue Jan 28, 2014 · 68 comments
Closed

Cross platform build failed #106

elgs opened this issue Jan 28, 2014 · 68 comments

Comments

@elgs
Copy link

elgs commented Jan 28, 2014

When I tried to build from OS X for Linux, it says:

$ GOOS=linux GOARCH=amd64 go install
# github.com/mattn/go-sqlite3
../../../mattn/go-sqlite3/sqlite3.c:241 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:256 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:296 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:309 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:339 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:342 unknown #: elif
../../../mattn/go-sqlite3/sqlite3.c:343 macro redefined: SQLITE_INT_TO_PTR
../../../mattn/go-sqlite3/sqlite3.c:344 macro redefined: SQLITE_PTR_TO_INT
../../../mattn/go-sqlite3/sqlite3.c:345 unknown #: elif
../../../mattn/go-sqlite3/sqlite3.c:346 macro redefined: SQLITE_INT_TO_PTR
../../../mattn/go-sqlite3/sqlite3.c:347 macro redefined: SQLITE_PTR_TO_INT
too many errors
@mattn
Copy link
Owner

mattn commented Jan 28, 2014

try to set CC=clang or CC=gcc

On 1/29/14, elgs [email protected] wrote:

When I tried to build from OS X for Linux, it says:
$ GOOS=linux GOARCH=amd64 go install

github.com/mattn/go-sqlite3

../../../mattn/go-sqlite3/sqlite3.c:241 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:256 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:296 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:309 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:339 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:342 unknown #: elif
../../../mattn/go-sqlite3/sqlite3.c:343 macro redefined: SQLITE_INT_TO_PTR
../../../mattn/go-sqlite3/sqlite3.c:344 macro redefined: SQLITE_PTR_TO_INT
../../../mattn/go-sqlite3/sqlite3.c:345 unknown #: elif
../../../mattn/go-sqlite3/sqlite3.c:346 macro redefined: SQLITE_INT_TO_PTR
../../../mattn/go-sqlite3/sqlite3.c:347 macro redefined: SQLITE_PTR_TO_INT
too many errors


Reply to this email directly or view it on GitHub:
#106

  • Yasuhiro Matsumoto

@elgs
Copy link
Author

elgs commented Jan 28, 2014

Seems to be the same:

$ CC=clang GOOS=linux GOARCH=amd64 go install
# github.com/mattn/go-sqlite3
../../../mattn/go-sqlite3/sqlite3.c:241 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:256 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:296 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:309 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:339 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:342 unknown #: elif
../../../mattn/go-sqlite3/sqlite3.c:343 macro redefined: SQLITE_INT_TO_PTR
../../../mattn/go-sqlite3/sqlite3.c:344 macro redefined: SQLITE_PTR_TO_INT
../../../mattn/go-sqlite3/sqlite3.c:345 unknown #: elif
../../../mattn/go-sqlite3/sqlite3.c:346 macro redefined: SQLITE_INT_TO_PTR
../../../mattn/go-sqlite3/sqlite3.c:347 macro redefined: SQLITE_PTR_TO_INT
too many errors
108:gsyncd elgs$ CC=gcc GOOS=linux GOARCH=amd64 go install
# github.com/mattn/go-sqlite3
../../../mattn/go-sqlite3/sqlite3.c:241 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:256 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:296 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:309 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:339 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:342 unknown #: elif
../../../mattn/go-sqlite3/sqlite3.c:343 macro redefined: SQLITE_INT_TO_PTR
../../../mattn/go-sqlite3/sqlite3.c:344 macro redefined: SQLITE_PTR_TO_INT
../../../mattn/go-sqlite3/sqlite3.c:345 unknown #: elif
../../../mattn/go-sqlite3/sqlite3.c:346 macro redefined: SQLITE_INT_TO_PTR
../../../mattn/go-sqlite3/sqlite3.c:347 macro redefined: SQLITE_PTR_TO_INT
too many errors

@ironsmile
Copy link

The same happens for me. I am using an amd64 linux and trying to compile for windows, os x and freebsd. All of them fail with the same message.

$ GOOS=windows GOARCH=amd64 go install
# github.com/mattn/go-sqlite3
./sqlite3.c:241 unknown #: if
./sqlite3.c:256 unknown #: if
./sqlite3.c:296 unknown #: if
./sqlite3.c:309 unknown #: if
./sqlite3.c:339 unknown #: if
./sqlite3.c:342 unknown #: elif
./sqlite3.c:343 macro redefined: SQLITE_INT_TO_PTR
./sqlite3.c:344 macro redefined: SQLITE_PTR_TO_INT
./sqlite3.c:345 unknown #: elif
./sqlite3.c:346 macro redefined: SQLITE_INT_TO_PTR
./sqlite3.c:347 macro redefined: SQLITE_PTR_TO_INT
too many errors
$ GOOS=darwin GOARCH=amd64 go install
# github.com/mattn/go-sqlite3
./sqlite3.c:241 unknown #: if
./sqlite3.c:256 unknown #: if
./sqlite3.c:296 unknown #: if
./sqlite3.c:309 unknown #: if
./sqlite3.c:339 unknown #: if
./sqlite3.c:342 unknown #: elif
./sqlite3.c:343 macro redefined: SQLITE_INT_TO_PTR
./sqlite3.c:344 macro redefined: SQLITE_PTR_TO_INT
./sqlite3.c:345 unknown #: elif
./sqlite3.c:346 macro redefined: SQLITE_INT_TO_PTR
./sqlite3.c:347 macro redefined: SQLITE_PTR_TO_INT
too many errors
$ GOOS=freebsd GOARCH=amd64 go install
# github.com/mattn/go-sqlite3
./sqlite3.c:241 unknown #: if
./sqlite3.c:256 unknown #: if
./sqlite3.c:296 unknown #: if
./sqlite3.c:309 unknown #: if
./sqlite3.c:339 unknown #: if
./sqlite3.c:342 unknown #: elif
./sqlite3.c:343 macro redefined: SQLITE_INT_TO_PTR
./sqlite3.c:344 macro redefined: SQLITE_PTR_TO_INT
./sqlite3.c:345 unknown #: elif
./sqlite3.c:346 macro redefined: SQLITE_INT_TO_PTR
./sqlite3.c:347 macro redefined: SQLITE_PTR_TO_INT
too many errors

I've tried CC=gcc and CC=clang. Nothing changes.

@mattn
Copy link
Owner

mattn commented Jan 28, 2014

Could you please try GOOS=linux GOARCH=amd64 go install -x ?

@elgs
Copy link
Author

elgs commented Jan 29, 2014

Still no luck.

$ go version
go version go1.2 darwin/amd64
32:gsyncd elgs$ GOOS=linux GOARCH=amd64 go -x install
flag provided but not defined: -x
Go is a tool for managing Go source code.

Usage:

    go command [arguments]

The commands are:

    build       compile packages and dependencies
    clean       remove object files
    env         print Go environment information
    fix         run go tool fix on packages
    fmt         run gofmt on package sources
    get         download and install packages and dependencies
    install     compile and install packages and dependencies
    list        list packages
    run         compile and run Go program
    test        test packages
    tool        run specified go tool
    version     print Go version
    vet         run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

    c           calling between Go and C
    gopath      GOPATH environment variable
    importpath  import path syntax
    packages    description of package lists
    testflag    description of testing flags
    testfunc    description of testing functions

Use "go help [topic]" for more information about that topic.

@ironsmile
Copy link

It must be GOOS=linux GOARCH=amd64 go install -x with the -x after install. I think that $CC does nothing for go. When I use it it still uses go's 6c. Example:

$ CC=gcc GOOS=windows GOARCH=amd64 go install -x
WORK=/tmp/go-build504111402
mkdir -p $WORK/github.com/mattn/go-sqlite3/_obj/
mkdir -p $WORK/github.com/mattn/
cd /home/iron4o/gopath/src/github.com/mattn/go-sqlite3
/home/iron4o/go/pkg/tool/linux_amd64/6c -F -V -w -I $WORK/github.com/mattn/go-sqlite3/_obj/ -I /home/iron4o/go/pkg/windows_amd64 -o $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3.6 -D GOOS_windows -D GOARCH_amd64 ./sqlite3.c
# github.com/mattn/go-sqlite3
./sqlite3.c:241 unknown #: if
./sqlite3.c:256 unknown #: if
./sqlite3.c:296 unknown #: if
./sqlite3.c:309 unknown #: if
./sqlite3.c:339 unknown #: if
./sqlite3.c:342 unknown #: elif
./sqlite3.c:343 macro redefined: SQLITE_INT_TO_PTR
./sqlite3.c:344 macro redefined: SQLITE_PTR_TO_INT
./sqlite3.c:345 unknown #: elif
./sqlite3.c:346 macro redefined: SQLITE_INT_TO_PTR
./sqlite3.c:347 macro redefined: SQLITE_PTR_TO_INT
too many errors

Also, I've found this https://code.google.com/p/go/issues/detail?id=4714 . Seems like it is on the subject. CGO_ENABLED is forcefully set to 0 when corss compiling for some reason.

@elgs
Copy link
Author

elgs commented Jan 29, 2014

Thanks @ironsmile, this time I got some more error messages:

$ GOOS=linux GOARCH=amd64 go install -x
WORK=/var/folders/lj/l5w0rd_j1vs9wdv438r9m96h0000gn/T/go-build265592758
mkdir -p $WORK/github.com/elgs/gsyncd/index/_obj/
mkdir -p $WORK/github.com/elgs/gsyncd/
cd /Volumes/User/Home/Dev/Go/gsyncd/src/github.com/elgs/gsyncd/index
/usr/local/Cellar/go/1.2/libexec/pkg/tool/darwin_amd64/6g -o $WORK/github.com/elgs/gsyncd/index/_obj/_go_.6 -p github.com/elgs/gsyncd/index -complete -D _/Volumes/User/Home/Dev/Go/gsyncd/src/github.com/elgs/gsyncd/index -I $WORK -I /Volumes/User/Home/Dev/Go/gsyncd/pkg/linux_amd64 ./index.go
mkdir -p $WORK/github.com/mattn/go-sqlite3/_obj/
mkdir -p $WORK/github.com/mattn/
cd /Volumes/User/Home/Dev/Go/gsyncd/src/github.com/mattn/go-sqlite3
/usr/local/Cellar/go/1.2/libexec/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/github.com/mattn/go-sqlite3/_obj/ -I /usr/local/Cellar/go/1.2/libexec/pkg/linux_amd64 -o $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3.6 -D GOOS_linux -D GOARCH_amd64 ./sqlite3.c
# github.com/mattn/go-sqlite3
../../../mattn/go-sqlite3/sqlite3.c:241 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:256 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:296 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:309 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:339 unknown #: if
../../../mattn/go-sqlite3/sqlite3.c:342 unknown #: elif
../../../mattn/go-sqlite3/sqlite3.c:343 macro redefined: SQLITE_INT_TO_PTR
../../../mattn/go-sqlite3/sqlite3.c:344 macro redefined: SQLITE_PTR_TO_INT
../../../mattn/go-sqlite3/sqlite3.c:345 unknown #: elif
../../../mattn/go-sqlite3/sqlite3.c:346 macro redefined: SQLITE_INT_TO_PTR
../../../mattn/go-sqlite3/sqlite3.c:347 macro redefined: SQLITE_PTR_TO_INT
too many errors
cd /Volumes/User/Home/Dev/Go/gsyncd/src/github.com/elgs/gsyncd/index
/usr/local/Cellar/go/1.2/libexec/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/github.com/elgs/gsyncd/index.a $WORK/github.com/elgs/gsyncd/index/_obj/_go_.6
mkdir -p /Volumes/User/Home/Dev/Go/gsyncd/pkg/linux_amd64/github.com/elgs/gsyncd/
cp $WORK/github.com/elgs/gsyncd/index.a /Volumes/User/Home/Dev/Go/gsyncd/pkg/linux_amd64/github.com/elgs/gsyncd/index.a

@elgs
Copy link
Author

elgs commented Jan 29, 2014

Now I'm wondering if it is not possible to do cross platform compile as it contains c program, so it is not in pure go?

@ironsmile
Copy link

Well It seems it is exactly that. For example when building for my native machine with CGO_ENABLED=0 I did get the same message:

$ CGO_ENABLED=0 go install -x
WORK=/tmp/go-build159155876
mkdir -p $WORK/github.com/mattn/go-sqlite3/_obj/
mkdir -p $WORK/github.com/mattn/
cd /home/iron4o/gopath/src/github.com/mattn/go-sqlite3
/home/iron4o/go/pkg/tool/linux_amd64/6c -F -V -w -I $WORK/github.com/mattn/go-sqlite3/_obj/ -I /home/iron4o/go/pkg/linux_amd64 -o $WORK/github.com/mattn/go-sqlite3/_obj/sqlite3.6 -D GOOS_linux -D GOARCH_amd64 ./sqlite3.c
# github.com/mattn/go-sqlite3
./sqlite3.c:241 unknown #: if
./sqlite3.c:256 unknown #: if
./sqlite3.c:296 unknown #: if
./sqlite3.c:309 unknown #: if
./sqlite3.c:339 unknown #: if
./sqlite3.c:342 unknown #: elif
./sqlite3.c:343 macro redefined: SQLITE_INT_TO_PTR
./sqlite3.c:344 macro redefined: SQLITE_PTR_TO_INT
./sqlite3.c:345 unknown #: elif
./sqlite3.c:346 macro redefined: SQLITE_INT_TO_PTR
./sqlite3.c:347 macro redefined: SQLITE_PTR_TO_INT
too many errors

On the other hand when I try to cross compile with CGO_ENABLED=1 I got the hardcoded message

$ CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go install -x
WORK=/tmp/go-build587075426
mkdir -p $WORK/runtime/cgo/_obj/
mkdir -p $WORK/runtime/
go build runtime/cgo: cannot use cgo when compiling for a different operating system

You can find it in the go build tool source:

[~/go/src]
iron4o$ search 'cannot use cgo when compiling for a different operating system'
cmd/go/build.go-    if goos != toolGOOS {
cmd/go/build.go:        return nil, nil, errors.New("cannot use cgo when compiling for a different operating system")
cmd/go/build.go-    }

I am thinking of removing this and rebuilding my go tools :D But that will be left for later.

@elgs
Copy link
Author

elgs commented Jan 29, 2014

@ironsmile WOW, what a hack you are going to do!
Now I think it's reasonable that by the nature of C, it might be not that easy to compile a Windows executable from a Mac, or Linux.

@mattn
Copy link
Owner

mattn commented Jan 30, 2014

@elgs Are you compiling windows executable!?

@mitchellhuang
Copy link

Yeah I'm also having the same issue... nothing seems to be working. I tried this
https://gist.github.com/steeve/6905542 and it still didn't compile.

@chr4
Copy link

chr4 commented Feb 18, 2014

Having the same issue with cross-building on osx for linux amd64

@jordan-wright
Copy link

I'm having a similar issues trying to cross-compile a windows executable from a 32-bit debian machine:

# github.com/mattn/go-sqlite3
../../mattn/go-sqlite3/sqlite3.c:92 unknown #: if
../../mattn/go-sqlite3/sqlite3.c:94 6c: No such file or directory: mingw.h
# github.com/mattn/go-sqlite3
../../mattn/go-sqlite3/sqlite3.c:92 unknown #: if
../../mattn/go-sqlite3/sqlite3.c:94 8c: No such file or directory: mingw.h

Looks to me like it's searching for a hardcoded mingw header file.

@elgs
Copy link
Author

elgs commented Mar 31, 2014

@mattn, sorry for a late reply. Yes. I'm compiling windows executable. Finally I was able to compile both win32 and win64 executables from mingw32, but with mingw64, I can only compile win64 executable.

@mattn
Copy link
Owner

mattn commented Apr 1, 2014

@elgs What error message did you get on mingw64?

@FiloSottile
Copy link
Contributor

As reported here
https://groups.google.com/forum/#!topic/golang-nuts/0pm23TkuCKQ
cgo is disabled on cross-compilation :(

@FiloSottile
Copy link
Contributor

Relevant Go issue: https://code.google.com/p/go/issues/detail?id=4714

@mattn
Copy link
Owner

mattn commented Apr 2, 2014

CL57100043 was merged. Still have problem?

@FiloSottile
Copy link
Contributor

Retrying with HEAD

@elgs
Copy link
Author

elgs commented Apr 2, 2014

@mattn I'm sorry I have wiped out my mingw64 environment as building that windows executable was a one off thing last time. I will try to reproduce the same problem again and come back here with my update.

@mattn
Copy link
Owner

mattn commented Apr 2, 2014

NP :)

@agonzalezro
Copy link

Hello!

I have the same problem that @jordan-wright had, have you been able to fix it?

I am trying to build my project here: http://gobuild.io/build/github.com/agonzalezro/polo (darwin/amd64) but I get this output:

...
github.com/mattn/go-sqlite3
# github.com/mattn/go-sqlite3
../../mattn/go-sqlite3/sqlite3.c:92 unknown #: if
../../mattn/go-sqlite3/sqlite3.c:94 6c: No such file or directory: mingw.h
[golang-sh]$ echo cleaning...
cleaning...
exit status 2

I will be happy to provide any more information than you need or do any other test, but "sadly", I am going to be disconnected for few days (sorry in advance).

Thanks!

@calmh
Copy link

calmh commented Jun 1, 2014

Adding to this, I see the same when trying to compile for 32 bits on 64 bit Linux (not trying to cross compile to another OS, although that would be nice too);

jb@udev2:~ $ go get github.com/mattn/go-sqlite3
jb@udev2:~ $ GOARCH=386 go get github.com/mattn/go-sqlite3
# github.com/mattn/go-sqlite3
src/github.com/mattn/go-sqlite3/sqlite3.c:92 unknown #: if
src/github.com/mattn/go-sqlite3/sqlite3.c:94 8c: No such file or directory: mingw.h
jb@udev2:~ $ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jb"
GORACE=""
GOROOT="/home/jb/go"
GOTOOLDIR="/home/jb/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
jb@udev2:~ $ 

@marcboeker
Copy link

As the fix under https://code.google.com/p/go/issues/detail?id=4714 should be now in the 1.3 release, is there any chance to get cross compilation working?

With Go 1.3 I'm getting ATM:

go build github.com/mattn/go-sqlite3: no buildable Go source files in /Users/marc/dev/go/src/github.com/mattn/go-sqlite3

I'm on OS X and trying to build for ARM

GOARCH=arm GOARM=6 GOOS=linux go build main.go

@gdy666
Copy link

gdy666 commented Jun 22, 2014

@mattn
Hello !
I have the same problem that @marcboeker had, have you been able to fix it?

@kc87654321
Copy link

@mattn
Hello !
I'm on XP and trying to build for LINUX With Go 1.3
get problem

github.com/mattn/go-sqlite3

d:\go\go\src\pkg\github.com\mattn\go-sqlite3\sqlite3.c:92 unknown #: if
d:\go\go\src\pkg\github.com\mattn\go-sqlite3\sqlite3.c:94 8c: No such file or directory: mingw.h
tanks

@mattn
Copy link
Owner

mattn commented Jul 17, 2014

Use goxc. And cross-compilable C compiler.

@mattn
Copy link
Owner

mattn commented Oct 24, 2014

Count you try this?

$ cat > foo.c
#include <mingw.h>
int main() {}
^D

$ arm-none-eabi-gcc foo.c

Your arm-c compiler seems having _HAVE_MINGW_H

@marcboeker
Copy link

$ arm-none-eabi-gcc foo.c
foo.c:1:19: fatal error: mingw.h: No such file or directory
 #include <mingw.h>
                   ^
compilation terminated.

I'm running OS X 10.10.

Is there a way to tell gcc to not define _HAVE_MINGW_H or _HAVE__MINGW_H?

#if defined(_HAVE_MINGW_H)
# include "mingw.h"
#elif defined(_HAVE__MINGW_H)
# include "_mingw.h"
#endif

I don't understand, why GOOS=linux triggers the include of mingw on my Mac?

$ GOOS=linux go build main.go
# github.com/mattn/go-sqlite3
/Users/marc/Temp/go/src/github.com/mattn/go-sqlite3/sqlite3.c:92 unknown #: if
/Users/marc/Temp/go/src/github.com/mattn/go-sqlite3/sqlite3.c:94 6c: No such file or directory: mingw.h

vs.

$ GOOS=darwin go build main.go
[success]

Thanks

@antonydevanchi
Copy link

Hey guys!

I have same problem with gcc and gocx. I don't have impressive experience with compiled languages and i hope that you can tell me what wrongs.

I've setup this package by "go get" and just include it to my source.

When i try compile it by "goxc" i get "../github.com/mattn/go-sqlite3/sqlite3.c:94 8c: No such file or directory: mingw.h".

@mattn you mentioned cross compiler, what is it? How i can install it and using? Why i've get this error message for all platforms exclude my host platform?

Thank you.

@antonydevanchi
Copy link

And if i run "GOOS=linux GOARCH=amd64 go build" complier works properly. I've got binary file.
But if i change GOARCH to 386 i've got the same problem related with mingw.h.

@antonydevanchi
Copy link

Okay, after 10 hours of researches i can do it with this link: http://www.limitlessfx.com/cross-compile-golang-app-for-windows-from-linux.html

@imheresamir
Copy link

@marcboeker I was struggling with cross-compiling for raspberry pi as well. I ended up biting the bullet and installing Go on the RPi using http://dave.cheney.net/unofficial-arm-tarballs and compiling natively. Make sure you set VRAM to around 16 so compilation doesn't take forever!

@marcboeker
Copy link

@imheresamir Thanks for the hint. Yeah, that's exactly what I'm doing at the moment and it is so slow :) But I'm going to try the VRAM option, hopefully this speeds things up. Thanks!

@mattn
Copy link
Owner

mattn commented Nov 14, 2014

@Smile42RU Thanks great post about cross compiling.
I've add link to you post on README.md

Thanks all!

@mattn mattn closed this as completed Nov 14, 2014
@billmccord
Copy link

Did anyone ever get this to cross-compile to Linux from a Mac OS X?

@marcboeker
Copy link

Nope, unfortunately not :(
I'm using a second, dedicated Raspberry Pi to do the job.

@hasty
Copy link

hasty commented Dec 16, 2014

@marcboeker I've had good luck using karalabe/xgo to cross-compile go-sqlite3 for ARM devices. Uses a docker linux container, but should be a lot faster than an RPi.

@marcboeker
Copy link

@hasty Awesome, thanks for the tip!

@hexdigest
Copy link

Hi, this is how I cross compile binary for freescale/arm on i386 architecture:

CC=/tools/m1sdk-tiny-1.0.0/toolchain/gcc-4.4.4-glibc-2.11.1-multilib-1.0/arm-fsl-linux-gnueabi/bin/arm-linux-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm go build main.go

@marcboeker
Copy link

Thanks!

@hit9
Copy link

hit9 commented Jan 7, 2016

I have the same problem on osx, attempting to build linux/amd64 version for my program.

ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)

@mattn
Copy link
Owner

mattn commented Jan 7, 2016

try with export CC=gcc.

@hit9
Copy link

hit9 commented Jan 7, 2016

@mattn Hello,

$ CC=gcc-4.9 GOOS=linux GOARCH=amd64 CGO_ENABLED=1 godep go build                                                                                                                                                 1 ↵ (go1.5.2 [email protected])
# runtime/cgo
gcc-4.9: warning: couldn't understand kern.osversion '14.3.0
# runtime/cgo
gcc-4.9: warning: couldn't understand kern.osversion '14.3.0
# runtime/cgo
gcc-4.9: warning: couldn't understand kern.osversion '14.3.0
/Users/hit9/github/oo/go/src/runtime/cgo/cgo.go:52:19: fatal error: errno.h: No such file or directory
compilation terminated.
godep: go exit status 2

@mattn
Copy link
Owner

mattn commented Jan 7, 2016

are you doing cross-compiling? when you need to use cross-compiler not host-compiler.

@hit9
Copy link

hit9 commented Jan 7, 2016

@mattn Thanks.

@jeffbmartinez
Copy link

jeffbmartinez commented Jun 15, 2016

Ran into similar issue cross-compiling a linux binary on osx. Near as I can tell in my mini investigation, cross-compiling would work except that osx's implementation of the linker tool (ld) doesn't understand the --build-id=none flag, which is where it chokes. Both gcc and clang attempt to pass this flag along to ld regardless of what you do with the CC, GCO_ENABLED, etc environment variables, so it's simply not gonna work.

I have a simple work around using docker which will hopefully help others who don't want to go through the trouble of recompiling a linux version of ld for osx:

Note: Naturally a docker workaround will require that you have docker installed :)

@mattn If you think it's reasonable feel free to add it to your readme where it will be more visible. I can make a PR as well if you like.

docker run --rm -v $GOPATH:/go -w /go/src/path/to/app golang:latest go build -v

This will put your executable in the expected place, in this case, $GOPATH/src/path/to/app.

You just need to update that /go/src/path/to/app part to be your actual directory containing your app, for example, /go/src/path/github.com/username/awesomeSqliteApp

If you want this working in a script like I have (for example I use a python fabric script to build and deploy automatically), you can do the following to prep docker on osx, since otherwise it requires the "Docker Quickstart Terminal":

eval $(docker-machine env default)
docker run --rm -v $GOPATH:/go -w /go/src/path/to/app golang:latest go build -v

In theory this should work on any platform that can run docker, though I've only tried on osx.

@mattn
Copy link
Owner

mattn commented Jun 16, 2016

@jeffbmartinez Thanks. Using docker is one of the way to do cross-compiling. So it's useful. If possible, could you please send me pull-request to update README.md?

@cprevallet
Copy link

Leaving this comment in the hopes it helps someone else in 2016.

Cross compiling Linux (Debian Jessie amd64) -> Windows (amd64) and OSX (amd64) worked for me with the following:

To Windows:
apt-get install gcc_mingw64
env CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go build $appname

To OSX:
Follow instructions at https://github.com/tpoechtrager/osxcross to build apple cross compiler
env CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 CC=o64-clang go build $appname

@oldfeel
Copy link

oldfeel commented Sep 3, 2016

@cprevallet Thank you, (ubuntu)work for windows.

apt-get install gcc-mingw-w64

@ikkemaniac
Copy link

ikkemaniac commented Jan 11, 2017

for documentation purposes:
in order to cross compile 32bit ubuntu on a 64bit ubuntu system libc6-dev-i386 is required.
Setting CC="gcc -m32" works.

@visoeclipse
Copy link

@jeffbmartinez use docker run --rm with sqlite3 , i cannot build , sth is wrong , go build -v is nothing with hold..

@guncha
Copy link

guncha commented Oct 7, 2017

Thanks @jeffbmartinez, it got me on the right track to get cross compilation working for Raspberry Pi 3. In case it's useful for anyone else, here's the Dockerfile I'm using for OSX:

FROM dockcross/linux-armv7:latest

# Largely copied from the official Golang image Dockerfile

RUN set -eux; \
  wget -O go.tgz "https://golang.org/dl/go1.9.1.linux-amd64.tar.gz"; \
  tar -C /usr/local -xzf go.tgz; \
  rm go.tgz; \
  export PATH="/usr/local/go/bin:$PATH"; \
	go version

ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH

RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
WORKDIR $GOPATH

And the command to build a package from the package directory:

PWD=$(pwd)
PROJECT="/go${PWD/"$GOPATH"/}"

docker run --rm -v $GOPATH:/go -w $PROJECT \
  -e "CGO_ENABLED=1" -e "GOARCH=arm" \
  go-arm-linux:latest go build .

@theblackturtle
Copy link

After some searching, I found a way to cross compilation on OSX:

  1. Install cross-compiler toolchains
brew install FiloSottile/musl-cross/musl-cross
brew install mingw-w64
  1. Build with -ldflags "-linkmode external -extldflags -static"

Ex:

GOOS=linux GOARCH=amd64 CC="/usr/local/bin/x86_64-linux-musl-gcc" CGO_ENABLED=1 go build -ldflags "-linkmode external -extldflags -static" -o [OUTPUT_BINARY]

Reference: https://blog.filippo.io/easy-windows-and-linux-cross-compilers-for-macos/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests