golang 交叉编译和语言互操作哦我的!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 交叉编译和语言互操作哦我的!相关的知识,希望对你有一定的参考价值。

#include "exportSyms.h"

int main() {

	PrintInt(10);
	LogString("Hello World");
	return 0;
}
Cross Compiling Go Code in Weird Configurations for fun and (little) profit
============================================================================

This represents my experiments in getting Go to do different configuration things for our cross-platform environment


Building Go code for Target (no mixed code, just Go)
----------------------------------------------------
```
nick@bignick:~/demo/targetMe$ GOOS=linux GOARCH=arm GOARM=5 go build 
nick@bignick:~/demo/targetMe$ file targetMe
targetMe: ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped
```

Building C code in to Go with cross compiler
--------------------------------------------
See also: https://github.com/golang/go/issues/12443#issuecomment-136906785 for useful information
```
nick@bignick:~/demo/$ CC=arm-poky-linux-gnueabi-gcc  CGO_CFLAGS="-march=armv7-a -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a8 --sysroot=/opt/poky/1.7.1/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi" CGO_LDFLAGS=-v CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=5 go build -o Foo_ARM *.go
```

Building Go code into C
-----------------------
```
nick@bignick:~/demo/cgodemo$ go build -buildmode=c-archive exportSyms.go 
nick@bignick:~/demo/cgodemo$ gcc -pthread useFromC.c exportSyms.a -o useFromC
nick@bignick:~/demo/cgodemo$ ./useFromC
10
2015/09/02 14:22:13 Hello World
```

Cross Compiling Go into Library for Compiling into Target app
-------------------------------------------------------------
```
nick@bignick:~/demo/cgodemo$ CC=arm-poky-linux-gnueabi-gcc  CGO_CFLAGS="-march=armv7-a -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a8 --sysroot=/opt/poky/1.7.1/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi" CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=5 go build  -buildmode=c-archive exportSyms.go
nick@bignick:~/demo/cgodemo$ $CC -pthread useFromC.c exportSyms.a -o useFromC
nick@bignick:~/demo/cgodemo$ file useFromC
useFromC: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=914408ad20be622e5205f93175e7c4d0337487e9, not stripped
nick@bignick:~/demo/cgodemo$ scp useFromC root@TARGET.local:/tmp/
...
root@TARGET:~# /tmp/useFromC 
10
2015/05/30 01:46:31 Hello World
```
package main

import (
    "C"
    "fmt"
    "log"
)

//export PrintInt
func PrintInt(x int) {
    fmt.Println(x)
}

//export LogString
func LogString(s * C.char) {
	log.Println(C.GoString(s))
}

func main() {}
package binding

// #include <stdlib.h>
// #include <stdio.h>
// #cgo LDFLAGS: -mfloat-abi=hard
/*	void myprint(char* s) {
		printf("%s", s);
	}
*/
import "C"

func Random() int {
	return int(C.random())
}

func PrintHello() {
	C.myprint(C.CString("Hello\n"))
}

func Seed(i int) {
	C.srandom(C.uint(i))
}
package main
import (
	"./binding"
	"fmt"
)

func main() {
	binding.PrintHello()
	binding.Seed(1)
	fmt.Println(binding.Random())
	binding.Seed(2)
	fmt.Println(binding.Random())
	binding.Seed(3)
	fmt.Println(binding.Random())
}

Golang交叉编译

golang的交叉编译要保证golang版本在1.5以上,本解决方案实例代码1.9版本执行的。

GOOS=linux GOARCH=amd64 go build hello.go

这里用到了两个变量:

  • GOOS:目标操作系统
  • GOARCH:目标操作系统的架构
OS ARCH OS Version
linux 386 / amd64 / arm >= Linux 2.6
darwin 386 / amd64 OS X (Snow Leopard + Lion)
freebsd 386 / amd64 >= FreeBSD 7
windows 386 / amd64 >= Windows 2000

扩展阅读

在网络上的诸多教程中可能会看到下面的编译命令

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build hello.go

其中CGO_ENABLED=0的意思是使用C语言版本的GO编译器,参数配置为0的时候就关闭C语言版本的编译器了。自从golang1.5以后go就使用go语言编译器进行编译了。在golang1.9当中没有使用CGO_ENABLED参数发现依然可以正常编译。当然使用了也可以正常编译。比如把CGO_ENABLED参数设置成1,即在编译的过程当中使用CGO编译器,我发现依然是可以正常编译的。

实际上如果在go当中使用了C的库,比如import "C"默认使用go build的时候就会启动CGO编译器,当然我们可以使用CGO_ENABLED=0来控制go build是否使用CGO编译器。

以上是关于golang 交叉编译和语言互操作哦我的!的主要内容,如果未能解决你的问题,请参考以下文章

Golang 交叉编译

Golang交叉编译

Golang的交叉编译问题

golang交叉编译笔记

Golang的交叉编译问题

Golang的交叉编译问题