1. Go编译器

两种官方编译器,gc和gccgo,其中gccgo基于gcc后端。

go编译器支持8种指令集,不同建构编译质量不同:

amd64 (also known )  
 (x86 or x86-)   Comparable to the amd64 port.
arm (ARM)
Supports Linux, FreeBSD, NetBSD, OpenBSD and Darwin binaries. Less widely used than the other ports.
arm64 (AArch64)
Supports Linux and Darwin binaries. New in 1.5 and not as well exercised as other ports.
ppc64, ppc64le (-bit PowerPC big- and little-endian)
Supports Linux binaries. New in 1.5 and not as well exercised as other ports.
mips, mipsle (-bit MIPS big- and little-endian)
Supports Linux binaries. New in 1.8 and not as well exercised as other ports.
mips64, mips64le (-bit MIPS big- and little-endian)
Supports Linux binaries. New in 1.6 and not as well exercised as other ports.
s390x (IBM System z)
Supports Linux binaries. New in 1.7 and not as well exercised as other ports.

go编译环境可以被定制,与平台和建构相关的是$GOOS和$GOARCH,分别指定目标操作系统和目标建构。常用组合如下:(注:$GOOS是darwin for macOS 10.1及以上和iOS)

$GOOS        $GOARCH
android     arm
darwin
darwin      amd64
darwin      arm
darwin      arm64
linux
linux       amd64
linux       arm
linux       arm64
windows
windows     amd64

go编译器(或go环境)安装

有两种安装方式:二进制发布包和源码包,参考https://golang.google.cn/doc/install。一般情况下可直接下载二进制发布包,官方已提供了常用平台的二进制发布包。

下载二进制tar包,tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz,一般安装路径为/usr/local,go工具命令要执行需要将/usr/local/go/bin导出到PATH环境变量中(/etc/profile可长期有效)。

源码包安装方式参考:Installing Go from source

ubuntu下可直接apt安装:

sudo apt-get install golang-go

go工具卸载

linux下直接删除/usr/local/go目录即可(同时修改PATH环境变量,或/etc/profile)。

GO环境变量

go env可打印go环境变量。

$GOPATH

GOPATH指定workspace位置,默认为$home/go,go项目在本地的开发环境的项目根路径(以便项目编译,go build, go install,go get)。若工作在其他目录,需设定GOPATH。注意GOPATH不能和go安装目录相同。

go env GOPATH

打印当前有效的GOPATH,若没有设置打印默认位置。

For convenience, add the workspace's bin subdirectory to your PATH:

$ export PATH=$PATH:$(go env GOPATH)/bin

GOPATH之下主要包含三个目录:bin,pkg,src。bin目录主要存放可执行文件;pkg目录存放编译好的库文件,主要是*.a文件;src目录下主要存放go的源文件。

$GOROOT

go的安装目录,配置后不会更改。一般为/usr/local/go或/usr/go或/usr/lib/go。

$GOROOT_FINAL

$GOOS and $GOARCH

用于不同平台的交叉编译,只需要在build之前设置这两个变量即可,这也是go语言的优势之一:可以编译生成跨平台运行的可执行文件。

注意:这个交叉编译暂不支持cgo方式,因此交叉编译时需要设置$CGO_ENABLED设置为0。

$GOHOSTOS and $GOHOSTARCH

$GOBIN

go二进制文件安装目录,默认为$GOROOT/bin。

$GO386

$GOARM

$GOMIPS

2. cgo

Cgo lets Go packages call C code.

The basics

If a Go source file imports "C", it is using cgo. The Go file will have access to anything appearing in the comment immediately preceding the line import "C", and will be linked against all other cgo comments in other Go files, and all C files included in the build process.

Note that there must be no blank lines in between the cgo comment and the import statement.

To access a symbol originating from the C side, use the package name C. That is, if you want to call the C function printf() from Go code, you write C.printf(). Since variable argument methods like printf aren't supported yet (issue 975), we will wrap it in the C method "myprint":

package main

/*
#include <stdio.h>
#include <stdlib.h>

void myprint(char* s) {
    printf("%s", s);
}
*/
import "C"

import "unsafe"

func main() {
    cs := C.CString("Hello from stdio\n")
    C.myprint(cs)
    C.free(unsafe.Pointer(cs))
}

参考:

1.  http://golang.org/doc/articles/c_go_cgo.html

2. https://github.com/golang/go/wiki/cgo

3. http://wiki.jikexueyuan.com/project/go-command-tutorial/0.13.html  极客学院go命令详解

3. Go交叉编译

参考:Go cross compilation

If cgo is not required (common go programs, not including c/c++)

The go tool won’t require any bootstrapping if cgo is not required. That allows you to target the following program to any GOOS/GOARCH without requiring you to do any additional work. Invoke go build.

$ cat main.go
package main
import "fmt"
func main() {
    fmt.Println("hello world")
}

In order to target android/arm, run the following command.

$ GOOS=android GOARCH=arm GOARM= go build .

The produced binary is targeting ARMv7 processors that runs Android. All possible GOOS and GOARCH values are listed on the environment docs.

If cgo is required (including c/c++)

If you need to have cgo enabled, the go tool allows you to provide custom C and C++ compilers via CC and CXX environment variables.

$ CGO_ENABLED= CC=android-armeabi-gcc CXX=android-armeabi-g++ \
    GOOS=android GOARCH=arm GOARM= go build .

The toolchain will invoke android-armeabi-gcc and android-armeabi-g++ if it is required to compile any part of the package with a C or C++ compiler. Consider the following program with a slightly different main function. Rather than outputting “hello world” to the standard I/O, it will use Android system libraries to write “hello world” to the system log.

$ cat main.go
// +build android

package main

// #cgo LDFLAGS: -llog
//
// #include <android/log.h>
//
// void hello() {
//   __android_log_print(
//     ANDROID_LOG_INFO, "MyProgram", "hello world");
// }
import "C"
func main() {
    C.hello()
}

If you build the program with the command above and examine the build with -x, you can observe that cgo is delegating the C compilation to arm-linux-androideabi-gcc.

$ CGO_ENABLED= \
CC=arm-linux-androideabi-gcc \
CXX=arm-linux-androideabi-g++ \
GOOS=android GOARCH=arm GOARM= go build -x .
...
CGO_LDFLAGS=”-g” “-O2” “-llog” /Users/jbd/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/github.com/rakyll/hello/_obj/ -importpath github.com/rakyll/hello — -I $WORK/github.com/rakyll/hello/_obj/ main.go
arm-linux-androideabi-gcc -I . -fPIC -marm -pthread -fmessage-length= -print-libgcc-file-name
arm-linux-androideabi-gcc -I . -fPIC -marm -pthread -fmessage-length= -I $WORK/github.com/rakyll/hello/_obj/ -g -O2 -o $WORK/github.com/rakyll/hello/_obj/_cgo_main.o -c $WORK/github.com/rakyll/hello/_obj/_cgo_main.c
...

Pre-building the standard library

The go tool also provides a utility if you would like to pre-build the standard library, targeting a specific GOOS and GOARCH.

$ CGO_ENABLED= \
    CC=arm-linux-androideabi-gcc \
    CXX=arm-linux-androideabi-g++ \
    GOOS=android GOARCH=arm GOARM= go install std

The standard library targeting android/armv7 will be available at $GOROOT/pkg/android_arm.

$ ls $GOROOT/pkg/android_arm
archive    fmt.a      math       runtime.a
bufio.a    go         math.a     sort.a
bytes.a    hash       mime       strconv.a
compress   hash.a     mime.a     strings.a
container  html       net        sync
crypto     html.a     net.a      sync.a
crypto.a   image      os         syscall.a
database   image.a    os.a       testing
debug      index      path       testing.a
encoding   internal   path.a     text
encoding.a io         reflect.a  time.a
errors.a   io.a       regexp     unicode
expvar.a   log        regexp.a   unicode.a
flag.a     log.a      runtime

If you prefer not to pre-build and install the standard library to the GOROOT, required libraries will be built while building user packages. But, the standard libraries builds are not preserved for future use at this stage and they will be rebuilt each time you run go build.

go编译的更多相关文章

  1. TODO:macOS编译PHP7.1

    TODO:macOS编译PHP7.1 本文主要介绍在macOS上编译PHP7.1,有兴趣的朋友可以去尝试一下. 1.下载PHP7.1源码,建议到PHP官网下载纯净到源码包php-7.1.0.tar.g ...

  2. Centos6.5下编译安装mysql 5.6

    一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | grep mysql 有的话通过下面的命令来卸载掉 rpm -e mysql //普通删除模式 rpm -e ...

  3. CENTOS 6.5 平台离线编译安装 PHP5.6.6

    一.下载php源码包 http://cn2.php.net/get/php-5.6.6.tar.gz/from/this/mirror 二.编译 编译之前可能会缺少一些必要的依赖包,加载一个本地yum ...

  4. CENTOS 6.5 平台离线编译安装 Mysql5.6.22

    一.下载源码包 http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.22.tar.gz 二.准备工作 卸载之前本机自带的MYSQL 安装 cmake,编 ...

  5. Android注解使用之注解编译android-apt如何切换到annotationProcessor

    前言: 自从EventBus 3.x发布之后其通过注解预编译的方式解决了之前通过反射机制所引起的性能效率问题,其中注解预编译所采用的的就是android-apt的方式,不过最近Apt工具的作者宣布了不 ...

  6. Hawk 6. 编译和扩展开发

    Hawk是开源项目,因此任何人都可以为其贡献代码.作者也非常欢迎使用者能够扩展出更有用的插件. 编译 编译需要Visual Stuido,版本建议使用2015, 2010及以上没有经过测试,但应该可以 ...

  7. android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!从此在andrid中自由使用 图像匹配、识别、检测

    目录: 1,过程感慨: 2,运行环境: 3,准备工作: 4,编译 .so 5,遇到的关键问题及其解决方法 6,实现效果截图. (原创:转载声明出处:http://www.cnblogs.com/lin ...

  8. 在Windows上编译和调试CoreCLR

    生成CoreCLR - Windows篇 本文的唯一目的就是让你运行Hello World 运行环境 Window 7+ Visual studio 2015 确保C++ 工具已经被安装,默认是不安装 ...

  9. 【踩坑速记】二次依赖?android studio编译运行各种踩坑解决方案,杜绝弯路,总有你想要的~

    这篇博客,只是把自己在开发中经常遇到的打包编译问题以及解决方案给大家稍微分享一下,不求吸睛,但求有用. 1.大家都知道我们常常会遇到dex超出方法数的问题,所以很多人都会采用android.suppo ...

  10. Windows下Visual studio 2013 编译 Audacity

    编译的Audacity版本为2.1.2,由于实在windows下编译,其源代码可以从Github上取得 git clone https://github.com/audacity/audacity. ...

随机推荐

  1. 【转】java io 流 设计模式

    知识点:什么是装饰模式: http://wenku.baidu.com/view/ad4eac9f51e79b896802263b.html(原理讲的很清楚) http://wenku.baidu.c ...

  2. linux 调用shell脚本传参

    例子: boolean execResult = true;        BufferedReader br = null;        try        {            //lin ...

  3. 【转载】Spring Cache介绍

    原文地址:http://www.cnblogs.com/rollenholt/p/4202631.html 缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个 ...

  4. 转 kafka架构简介

    kafka架构 转 http://www.cnblogs.com/chushiyaoyue/p/5612298.html 相关文章: https://www.jianshu.com/p/6233d53 ...

  5. django http404 详解

    [引子] 今天在看django的官方文档的时候看到get_object_or_404这个函数感觉比较奇怪.这个主要来自于它的功能,如果要查询的对象 存在那么就返回对象:如果对象不存在那么就要报404 ...

  6. influxdb 配置文件注释

    ### Welcome to the InfluxDB configuration file. # The values in this file override the default value ...

  7. 在 Unity 中基于 Oculus DK1 的开发

    开发环境: Windows 10 专业版 64位(GeForce GTX 970M,驱动版本:378.72) 大朋助手 1.3.2.10,大朋E2(http://www.deepoon.com/dap ...

  8. Invalid configuation file. File "**********" was created by a VMware product with more feature than this version of VMware Workstation and cannot be

    大概就是说你的之前用来创建虚拟机的VM版本太高,被移植的VM版本太低.所以你需要改一点东西. 打开你的虚拟机的目录(不是VM的),然后看到你很多文件. 然后你看到*.vmx的文件(实在找不到就按文件类 ...

  9. Windows 以管理员运行而不提示

    组策略中设置一下:“计算机配置”-“Windows 配置”-“安全设置”-“本地策略”-“安全选项”下:修改“用户帐户控制: 在管理审批模式下管理员的提升提示行为”选项为“提示凭据”就会再弹出提示框了 ...

  10. 【Unity】4.3 地形编辑器

    分类:Unity.C#.VS2015 创建日期:2016-04-10 一.简介 Unity拥有功能完善的地形编辑器,支持以笔刷绘制的方式实时雕刻出山脉.峡谷.平原.高地等地形.Unity地形编辑器同时 ...