duanqian3464
2018-12-08 00:58
浏览 78
已采纳

使用os / exec命令从Windows交叉编译到Linux

The title mostly says it. I know that I can do

set GOOS=linux
set GOARCH=amd64

in cmd before I go build, but I'm trying to write a build script and do it all with exec.Command.

My go build -o etc works with exec.Command (it builds), but when printing out GOOS in a test script after either of this commands:

cmd := exec.Command("set", "GOOS=linux")
// OR
cmd := exec.Command("set GOOS=linux")

I get windows.

Any ideas? Thanks!

图片转代码服务由CSDN问答提供 功能建议

标题大多是这样说的。 我知道我可以在使用cmd之前

  set GOOS = linux 
set GOARCH = amd64 
   
 
 

go build ,但是我试图编写一个构建脚本并使用exec.Command来完成所有操作。

我的 go build -o etc < / code>与exec.Command一起使用(它会生成),但是在以下任何一个命令之后在测试脚本中打印出GOOS时:

  cmd:= exec.Command(“  set“,” GOOS = linux“)
 // OR 
cmd:= exec.Command(” set GOOS = linux“)
   
 
 

我得到 windows

有什么想法吗? 谢谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanqia9034 2018-12-08 06:41
    已采纳

    I strongly suggest you just use a tool like Gox instead. It cross-compiles into every supported build target by just executing gox. It also has options if you only want to build for certain targets.

    As for the code you're asking about, exec.Command doesn't create a new process, or really do anything other than create a Cmd struct. The os/exec documentation (specifically the docs for the Command function) has an example of what you seem to be trying to do- execute another program with custom environment variable assignments... see the Example (Environment) section of the linked documentation and try to follow that structure if you still want to cross-compile your way.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题