[英]Cross compile from Windows to Linux using os/exec Command
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. 在进行
go build
之前在cmd中使用cmd,但是我试图编写一个构建脚本并使用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: 我的
go build -o etc
可以与exec.Command一起使用(它可以构建),但是在以下任何一个命令之后在测试脚本中打印出GOOS时:
cmd := exec.Command("set", "GOOS=linux")
// OR
cmd := exec.Command("set GOOS=linux")
I get windows
. 我有
windows
。
Any ideas? 有任何想法吗? Thanks!
谢谢!
I strongly suggest you just use a tool like Gox instead. 我强烈建议您改用Gox之类的工具。 It cross-compiles into every supported build target by just executing
gox
. 只需执行
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. 至于您要查询的代码,
exec.Command
不会创建新进程,或者除了创建Cmd
结构以外,实际上没有做任何其他事情。 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. os / exec文档(特别是Command函数的文档)提供了您似乎正在尝试执行的操作的示例-使用自定义环境变量分配执行另一个程序...请参阅链接的文档的“
Example (Environment)
部分,如果您仍然想交叉编译您的方法,请尝试遵循该结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.