Go Cross-Compilation Mac to Linux: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= =Overview= Set <code>GOOS</code> and <code>GOARCH</code> environment variables to appropriate...")
 
 
(2 intermediate revisions by the same user not shown)
Line 10: Line 10:
export GOARCH=amd64
export GOARCH=amd64
</syntaxhighlight>
</syntaxhighlight>
=Cgo=
{{External|https://go.dev/blog/cgo}}
If Cgo is needed:
<syntaxhighlight lang='bash'>
brew install FiloSottile/musl-cross/musl-cross
</syntaxhighlight>
<syntaxhighlight lang='bash'>
export CC=x86_64-linux-musl-gcc; export CXX=x86_64-linux-musl-g++;  export GOOS=linux; export GOARCH=amd64; export CGO_ENABLED=1; go build [...]
</syntaxhighlight>
<font color=darkkhaki>Even if the program seems to be compiling successfully, and the executable is produced, when trying to run it from a Linux VM I get bash: ./yunikorn-scheduler: No such file or directory, which tells me that something is wrong with the file.</font>

Latest revision as of 03:56, 9 December 2023

Internal

Overview

Set GOOS and GOARCH environment variables to appropriate values, during the build.

To build on Mac for Linux:

export GOOS=linux
export GOARCH=amd64

Cgo

https://go.dev/blog/cgo

If Cgo is needed:

brew install FiloSottile/musl-cross/musl-cross
export CC=x86_64-linux-musl-gcc; export CXX=x86_64-linux-musl-g++;  export GOOS=linux; export GOARCH=amd64; export CGO_ENABLED=1; go build [...]

Even if the program seems to be compiling successfully, and the executable is produced, when trying to run it from a Linux VM I get bash: ./yunikorn-scheduler: No such file or directory, which tells me that something is wrong with the file.