加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

如何在Linux上编译跨平台的Go语言项目?

发布时间:2020-12-14 00:53:57 所属栏目:Linux 来源:网络整理
导读:我正在尝试在 Linux上设置我的Go编译器,它可以为任何其他架构或平台编译项目.我正在使用官方Ubuntu 14.04存储库中的默认包,我使用的是64位系统.此配置允许我仅针对Linux进行编译,仅针对64位系统进行编译.至少我想编译32位Linux甚至32位Windows系统.有可能以
我正在尝试在 Linux上设置我的Go编译器,它可以为任何其他架构或平台编译项目.我正在使用官方Ubuntu 14.04存储库中的默认包,我使用的是64位系统.此配置允许我仅针对Linux进行编译,仅针对64位系统进行编译.至少我想编译32位Linux甚至32位Windows系统.有可能以某种方式吗?

另外一件事是我想使用两个Go绑定:
https://github.com/mattn/go-gtk和https://github.com/mattn/go-webkit

我正在使用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&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=osaka&amp;aq=&amp;sll=34.885931,-115.180664&amp;sspn=29.912003,39.506836&amp;brcurrent=3,0x6000e86b2acc70d7:0xa399ff48811f596d,0&amp;ie=UTF8&amp;hq=&amp;hnear=%E5%A4%A7%E9%98%AA%E5%BA%9C%E5%A4%A7%E9%98%AA%E5%B8%82&amp;ll=34.693738,135.502165&amp;spn=0.471406,0.617294&amp;z=11&amp;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,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,0)

    button = gtk.NewButtonWithLabel("load HTML String")
    button.Clicked(func() {
        webview.LoadHtmlString(HTML_STRING,0)

    button = gtk.NewButtonWithLabel("Google Maps")
    button.Clicked(func() {
        webview.LoadHtmlString(MAP_EMBED,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

我收到此错误:

webview.go:5:2: no buildable Go source files in
/home/yeeapple/Documents/Coding/Go/Source/src/github.com/mattn/go-gtk/gtk

webview.go:6:2: no buildable Go source files in
/home/yeeapple/Documents/Coding/Go/Source/src/github.com/mattn/go-webkit/webkit

我看到的另一件事是在GOPATH目录和pkg文件夹中只有带有* .a文件的“linux_amd64”文件夹.

例如,如果没有其他导入,我可以为其他系统编译Go文件.跨平台编译工作正常:

package main

import "fmt"

func main() {
    fmt.Printf("hello,worldn")
}

版本:

$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.

解决方法

使用Go 1.2,交叉编译代码时将禁用cgo功能.这意味着任何包含导入“C”的源文件都不会被编译,这会使许多软件包无法使用.因此,当您简单的“hello world”程序编译时,使用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环境变量将Go指向适当的副本.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读