Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make `go get` to build against x86_64 instead of i386

Tags:

go

I am trying to use either go-qml or gotk3 to build a very simple desktop app that can run under OS X. However when I try to use go get to install either library, it will try to build for i386 and skip the libraries that were build against x86_64. I could try to get the 32 bit version of those libraries, but I would prefer to build for 64bit. How do I instruct go get to do so?

The warnings that are followed by errors look lie this:

go get gopkg.in/qml.v1
# gopkg.in/qml.v1
ld: warning: ld: warning: ld: warning: ignoring file /usr/local/Cellar/qt5/5.3.2/lib/QtWidgets.framework/QtWidgets, file was built for x86_64 which       is not the architecture being linked (i386): /usr/local/Cellar/qt5/5.3.2/lib/QtWidgets.framework/QtWidgetsignoring file /usr/local/Cellar/qt5/5.3.2/lib/QtGui.framework/QtGui, file was built for x86_64 which is not the architecture being linked (i386): /usr/local/Cellar/qt5/5.3.2/lib/QtGui.framework/QtGuiignoring file /usr/local/Cellar/qt5/5.3.2/lib/QtQuick.framework/QtQuick, file was built for x86_64 which is not the architecture being linked (i386): /usr/local/Cellar/qt5/5.3.2/lib/QtQuick.framework/QtQuick
like image 877
ajmurmann Avatar asked Nov 15 '14 23:11

ajmurmann


People also ask

Is AMD64 the same as x86_64?

"AMD64" is the name chosen by AMD for their 64-bit extension to the Intel x86 instruction set. Before release, it was called "x86-64" or "x86_64", and some distributions still use these names.

What is go build command?

The go build command compiles the packages, along with their dependencies, but it doesn't install the results. The go install command compiles and installs the packages.

How can you make a file build only on Windows in go?

You can set either the GOOS or GOARCH environment variables to build for a different platform using go build . If you are not using a Windows system, build a windows binary of app by setting the GOOS environment variable to windows when running the go build command: GOOS=windows go build.

What is Goos and Goarch?

GOOS refers to the operating system (Linux, Windows, BSD, etc.), while GOARCH refers to the architecture to build for. $ env GOOS=linux GOARCH=arm64 go build -o prepnode_arm64.


2 Answers

Set the environment variable GOARCH to the value amd64. This instructs the go command to generate files for amd64. Other valid values for GOARCH are 386 and arm.

like image 81
fuz Avatar answered Sep 18 '22 10:09

fuz


F.Y.I.

The Go compilers support the following instruction sets:

  • amd64, 386
    • The x86 instruction set, 64- and 32-bit.
  • arm64, arm
    • The ARM instruction set, 64-bit (AArch64) and 32-bit.
  • mips64, mips64le, mips, mipsle
    • The MIPS instruction set, big- and little-endian, 64- and 32-bit.
  • ppc64, ppc64le
    • The 64-bit PowerPC instruction set, big- and little-endian.
  • riscv64
    • The 64-bit RISC-V instruction set.
  • s390x
    • The IBM z/Architecture.
  • wasm
    • WebAssembly.

(from: Introduction | Installing Go from source | Doc @ golang.org)

Also, you can go tool dist list to check the available architectures to build in your machine.

$ go tool dist list
aix/ppc64
android/386
android/amd64
android/arm
android/arm64
darwin/amd64
darwin/arm64
dragonfly/amd64
freebsd/386
(* snip *)
    

To build a static binary for macOS (Intel/ARM64) would be as below. In this manner, I suppose GOOS="darwin" GOARCH="arm64" combination will be for M1 architecture.

MyVar="foo"

CGO_ENABLED=0 \
        GOOS="darwin" \
        GOARCH="amd64" \
        GOARM="" \
        go build \
        -ldflags="-s -w -extldflags \"-static\" -X 'main.myVar=${MyVar}'" \
        -o="/path/to/export/bin/myApp" \
        "/path/to/main.go"

To compile for Linux on ARM v6, such as RaspberryPi Zero W, the combination would be as below.

$ CGO_ENABLED=0 GOOS="linux" GOARCH="arm" GOARM="6" go build .
like image 43
KEINOS Avatar answered Sep 21 '22 10:09

KEINOS