我试图在Linux上设置Go编译器,它可以为任何其他架构或平台编译项目。我使用的是来自官方Ubuntu14.04存储库的默认包,我使用的是64位系统。这种配置允许我只为Linux编译,也只为64位系统编译。至少我想编译32位Linux,甚至32位Windows系统。有可能做些什么吗?
另外一件事是,我想使用两个Go绑定:https://github.com/mattn/go-gtk和https://github.com/mattn/go-webkit
我正在使用示例代码进行测试:
package main
import (
"os"
"github.com/mattn/go-gtk/gtk"
"github.com/mattn/go-webkit/webkit"
)
const HTML_STRING = `
<doctype html>
<meta charset="utf-8"/>
<style>
div { font-size: 5em }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function() {
$('#hello1').slideDown('slow', function() {
$('#hello2').fadeIn()
})
})
</script>
<div id="hello1" style="display: none">Hello</div>
<div id="hello2" style="display: none">世界</div>
</div>
`
const MAP_EMBED = `
<style> *{ margin : 0; padding : 0; } </style>
<iframe width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.jp/maps?f=q&source=s_q&hl=en&geocode=&q=osaka&aq=&sll=34.885931,-115.180664&sspn=29.912003,39.506836&brcurrent=3,0x6000e86b2acc70d7:0xa399ff48811f596d,0&ie=UTF8&hq=&hnear=%E5%A4%A7%E9%98%AA%E5%BA%9C%E5%A4%A7%E9%98%AA%E5%B8%82&ll=34.693738,135.502165&spn=0.471406,0.617294&z=11&output=embed"></iframe>
`
func main() {
gtk.Init(nil)
window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
window.SetTitle("webkit")
window.Connect("destroy", gtk.MainQuit)
vbox := gtk.NewVBox(false, 1)
entry := gtk.NewEntry()
entry.SetText("http://golang.org/")
vbox.PackStart(entry, false, false, 0)
swin := gtk.NewScrolledWindow(nil, nil)
swin.SetPolicy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
swin.SetShadowType(gtk.SHADOW_IN)
webview := webkit.NewWebView()
webview.Connect("load-committed", func() {
entry.SetText(webview.GetUri())
})
swin.Add(webview)
vbox.Add(swin)
entry.Connect("activate", func() {
webview.LoadUri(entry.GetText())
})
button := gtk.NewButtonWithLabel("load String")
button.Clicked(func() {
webview.LoadString("hello Go GTK!", "text/plain", "utf-8", ".")
})
vbox.PackStart(button, false, false, 0)
button = gtk.NewButtonWithLabel("load HTML String")
button.Clicked(func() {
webview.LoadHtmlString(HTML_STRING, ".")
})
vbox.PackStart(button, false, false, 0)
button = gtk.NewButtonWithLabel("Google Maps")
button.Clicked(func() {
webview.LoadHtmlString(MAP_EMBED, ".")
})
vbox.PackStart(button, false, false, 0)
window.Add(vbox)
window.SetSizeRequest(600, 600)
window.ShowAll()
proxy := os.Getenv("HTTP_PROXY")
if len(proxy) > 0 {
soup_uri := webkit.SoupUri(proxy)
webkit.GetDefaultSession().Set("proxy-uri", soup_uri)
soup_uri.Free()
}
entry.Emit("activate")
gtk.Main()
}
如果我用
go build
如果我试图用其他设置编译它:
GOOS=windows GOARCH=386 go build
GOARCH=386 go build
我知道这个错误:
/home/yeeapple/Documents/Coding/Go/Source/src/github.com/mattn/go-gtk/gtk中的webview.go:5:2:不可构建的Go源文件 /home/yeeapple/Documents/Coding/Go/Source/src/github.com/mattn/go-webkit/webkit中的webview.go:6:2:不可构建的Go源文件
我看到的另一件事是,在GOPATH目录和pkg文件夹中,只有带有*.a文件的"linux_amd64“文件夹。
例如,如果其他系统没有额外的导入,我可以编译Go文件。跨平台编译工作很好,包括:
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
版本:
$ go version
go version go1.2.1 linux/amd64
$ gccgo --version
gccgo (Ubuntu 4.9-20140406-0ubuntu1) 4.9.0 20140405 (experimental) [trunk revision 209157]
Copyright (C) 2014 Free Software Foundation, Inc.
发布于 2014-06-22 12:56:24
使用Go 1.2时,交叉编译代码时将禁用cgo
特性。这意味着任何包含import "C"
的源文件都不会被编译,这使得许多包无法使用。因此,当您的简单"hello“程序编译时,使用cgo
的等效程序将失败:
package main
/*
#include <stdlib.h>
#include <stdio.h>
*/
import "C"
import "unsafe"
func main() {
hello := C.CString("Hello world")
defer C.free(unsafe.Pointer(hello))
C.puts(hello)
}
在amd64 Linux系统上,如果您试图为x86编译,就会遇到构建失败:
$ GOARCH=386 go build hello.go
can't load package: no buildable Go source files in /...
要编译这个程序,还需要设置环境变量CGO_ENABLED=1
来手动启用cgo
。这将导致它试图编译程序,但是如果您没有安装x86编译器,它仍然会失败。由于您说您正在使用Ubuntu,所以可以通过安装gcc-multilib
包来做到这一点:
$ sudo apt-get install gcc-multilib
安装该程序后,您应该能够编译该程序:
$ GOARCH=386 CGO_ENABLED=1 go build hello.go
$ file hello
hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=..., not stripped
虽然Ubuntu附带了Windows交叉编译工具链(在mingw32
包中),但是编译Windows二进制文件仍然有困难:
$ GOOS=windows GOARCH=386 CGO_ENABLED=1 go build hello.go
go build runtime/cgo: cannot use cgo when compiling for a different operating system
这张支票在GO 1.3中不再存在,所以您可能会对该版本有一些运气。
现在,让交叉编译器工具链可用只是战斗的一部分:您还需要为目标体系结构编译的依赖关系版本。对于像WebKit这样大的东西来说,这并不简单,但是您可能可以使用CFLAGS
、LDFLAGS
和PKG_CONFIG_PATH
环境变量指向适当的副本。
https://stackoverflow.com/questions/24350698
复制相似问题