Updated Cross Compiling (markdown)

Hajime Hoshi 2018-01-29 03:17:54 +09:00
parent c1c2218399
commit ae0bdbcbd7

@ -1,21 +1,33 @@
### If you wish to cross compile from a Linux system to Windows you can quite easily do so.
**If you wish to cross compile from a Linux system to Windows you can quite easily do so.**
Start by installing sys/windows from the standard library
`go get golang.org/x/sys/windows`
Start by installing sys/windows from the standard library:
Then you'll need the mingw compiler.
* Debian/Ubuntu:
`sudo apt install mingw-w64`
_Note:_ If package cannot be found on ubuntu make sure you've added the multiverse repository
(`sudo add-apt-repository multiverse`)
```sh
go get golang.org/x/sys/windows
```
When completed you should be able to cross compile using this:
`CGO_ENABLED=1 CXX=x86_64-w64-mingw32-g++ CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 go build file.go`
Then you'll need the mingw compiler. For example, on Debian/Ubuntu, run this command:
```sh
sudo apt install mingw-w64
```
_Note:_ If package cannot be found on ubuntu make sure you've added the multiverse repository (`sudo add-apt-repository multiverse`)
When completed you should be able to cross compile using this:
```sh
CGO_ENABLED=1 CXX=x86_64-w64-mingw32-g++ CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 go build file.go
```
## Tips
Since that's quite a messy command to use each time you want to test your program you might wanna set up a bash alias in your .bashrc or .profile file.
* `nano ~/.bashrc`
* add this line
`alias LinuxtoWindows="export CXX=x86_64-w64-mingw32-g++ && export CC=x86_64-w64-mingw32-gcc && CGO_ENABLED=1 && GOOS=windows"`
to the bottom of the file.
* add the below line to the bottom of the file:
Now you may simply use `LinuxToWindows && go build file.go` to cross compile
```sh
alias LinuxtoWindows="export CXX=x86_64-w64-mingw32-g++ && export CC=x86_64-w64-mingw32-gcc && CGO_ENABLED=1 && GOOS=windows"
```
Now you may simply use `LinuxToWindows && go build file.go` to cross compile.