How to CrossCompile Go programs on Windows 10

前端 未结 2 1639
一个人的身影
一个人的身影 2020-12-15 11:21

I want to compile a Go programm for a linux machine. I always used that way which is described here:

How to cross compile from Windows to Linux?

That worked

相关标签:
2条回答
  • 2020-12-15 12:13

    I faced the same problem

    Install this package

    https://www.npmjs.com/package/cross-env

    and run

    cross-env GOOS=linux go build -o hello
    
    0 讨论(0)
  • 2020-12-15 12:16

    set is an internal command of the Windows command line interpreter (cmd.exe).

    If you're using PowerShell, then changing values of environment variables should be done like:

    $Env:<variable-name> = "<new-value>"
    

    For more details, see PowerShell documentation: About Environment Variables

    So to change GOOS, use:

    $Env:GOOS = "linux"
    

    To do a cross-compilation:

    1. Navigate to the folder where the main package is.

    2. Run $Env:GOOS = "linux"

    3. Optionally run $Env:GOARCH = "amd64"

    4. Run go build

    Or you can do it in a single line:

    $Env:GOOS = "linux"; $Env:GOARCH = "amd64"; go build
    

    To specify the output file name:

    $Env:GOOS = "linux"; $Env:GOARCH = "amd64"; go build -o hello
    
    0 讨论(0)
提交回复
热议问题