转载 CGO arm 树莓pi下的交叉编译

https://medium.com/@chrischdi/cross-compiling-go-for-raspberry-pi-dc09892dc745

Cross-compiling Go for Raspberry Pi

chrischdi

Nov 25, 2018·4 min read

This post is about how to cross-compile Go applications for other architectures. Cross-compiling Go applications could get quiet challenging when we have dependencies to modules which depend on cgo and on C-libraries.

Update: I’ve added a section at the end.

Introduction

I’m currently writing a small Go application which has a gui implemented by using github.com/gotk3/gotk3 as library for gtk bindings. Compiling on a Raspberry Pi is quiet slow compared to my laptop, which is already running for some years.

Why don’t I just cross-compile the application on my laptop

Yes cross-compiling could finally speed up my development workflow and is quiet easy for Go when there are no dependencies to cgo or even libraries used by cgo.

I’d like to share the different ways how to cross-compile Go for the Raspberry Pi.

Compiling without cgo

Cross-compiling Go-applications for other architectures like aarch64 is quiet easy. Consider the following simple hello-world Go application:

hello-world Go application

You can easily cross-compile this for a Raspberry Pi and run it there:

$ env GOARCH=arm64 GOOS=linux go build -o hello-world main.go
$ scp hello-world rpi:~/hello-world
$ ssh rpi ./hello-world
hello world

As long as we are writing applications which do not need cgo this works very well.

Compiling with cgo

Let’s try a small example including cgo::

Let’s try to build it for the Raspberry Pi the same way we did without cgo:

$ env GOARCH=arm64 GOOS=linux CGO_ENABLED=1 go build -o cgo main.go
# runtime/cgo
gcc: error: unrecognized command line option ‘-marm’; did you mean ‘-mabm’?

This fails because go tries to use the default gcc compiler of our operating system. But in this case we need to use a compiler which builds for arm. On archlinux we can install aarch64-linux-gnu-gcc and also pass the name of the compiler to the go build-command:

$ export CC=aarch64-linux-gnu-gcc
$ GOARCH=arm64 GOOS=linux CGO_ENABLED=1 go build -o cgo main.go
$ scp hello-world rpi:~/hello-world
$ ssh rpi ./cgo
hello world cgo

Adding libraries for cgo

As I noted at the beginning, I want to use github.com/gotk3/gotk3.

So let’s implement a small user interface and cross-compile it for the Raspberry Pi:

$ export CC=aarch64-linux-gnu-gcc
$ env GOARCH=arm64 GOOS=linux CGO_ENABLED=1 go build -o cgo main.go
# github.com/gotk3/gotk3/glib
/usr/lib/gcc/aarch64-linux-gnu/8.2.0/../../../../aarch64-linux-gnu/bin/ld: cannot find -lgio-2.0
/usr/lib/gcc/aarch64-linux-gnu/8.2.0/../../../../aarch64-linux-gnu/bin/ld: cannot find -lgobject-2.0
/usr/lib/gcc/aarch64-linux-gnu/8.2.0/../../../../aarch64-linux-gnu/bin/ld: cannot find -lglib-2.0
collect2: error: ld returned 1 exit status

So what went wrong here? Let’s assume the necessary libraries are installed on the x86 system we’re working on. The problem is that these are not for the cross-compile toolchain.

We also have a Raspberry Pi running the aarch64 architecture and having the libraries installed. Let’s mount the file system of the sdcard which is normally used by the Raspberry Pi and pass the path to the go compiler:

$ mount /dev/sdc2 /mnt
$ export CC=aarch64-linux-gnu-gcc
$ export CGO_CFLAGS="--sysroot=/mnt"
$ export CGO_LDFLAGS="--sysroot=/mnt"
$ env GOARCH=arm64 GOOS=linux CGO_ENABLED=1 go build -o cgo main.go

That’s it.

Of course we could copy the content of the Raspberry Pi’s file system somewhere on our disk so we don’t have to mount the sdcard all the time and keep the Raspberry Pi running while we are cross-compiling.

cgo & pkg-build

Note: this section was added later on

After writing this blog post and compiling for aarch64 I recognized that the GPIO support on the Raspberry Pi at aarch64 does not work well. Afterwards I tried to cross-compile my application for armv7 .

Long story short: I had to learn that Go is using its own pkg-config (see docs) to gather build dependency information.

To cross-compile my application for armv7 I have used the precompiled toolchain from archlinuxARM and added the environment variables PKG_CONFIG_DIRPKG_CONFIG_LIBDIR and PKG_CONFIG_SYSROOT_DIR.

Now I’m compiling successfully for armv7 using the following command:

$ mount /dev/sdc2 /mnt
$ export CC=aarch64-linux-gnu-gcc
$ export CGO_CFLAGS="--sysroot=/mnt"
$ export CGO_LDFLAGS="--sysroot=/mnt"
$ export CC="path/to/toolchain/arm-unknown-linux-gnueabihf/bin/arm-unknown-linux-gnueabihf-gcc
$ env \
PKG_CONFIG_DIR="" \
PKG_CONFIG_LIBDIR="/mnt/usr/lib/pkgconfig:/mnt/usr/share/pkgconfig"\
PKG_CONFIG_SYSROOT_DIR="/mnt" \
GOARCH=arm \
GOARM=7 \
GOOS=linux \
CGO_ENABLED=1 \
go build -o app ./

Addition

Archlinux directly offers the aarch64-linux-gnu-gcc as a package but this is not the case for the armv7 or other architectures and I don’t know whether or not the compilers are easily available on other distributions. crosstool-ng is able to create cross-compiling toolchains for different architectures. There are prebuilt cross-compilers available at the Cross-Compiling documentation of the Archlinux ARM website and they also explain how to build your own cross-compiler using crosstool-ng.

CGO arm 树莓pi下的交叉编译相关推荐

  1. 完整的Python 3和树莓Pi大师课 Complete Python 3 and Raspberry Pi Masterclass

    在一门课程中学习Python 3基础.高级Python.科学Python.树莓Pi和硬件项目 你会学到: Python 3基础 Python 3高级概念 树莓皮的设置和使用 科学巨蟒生态系统 NumP ...

  2. ARM开发板下Qt实现中文输入法的波折历程

    ** ARM开发板下Qt实现中文输入法的波折历程 ** 在移植软键盘输入法时候,如果用到中文输入法一定会用到数据库,移植的Qt工程运行时如果碰到如下error,一定要看看我的文章,对你会有所帮助!!! ...

  3. Ubuntu16.04下arm-linux-gcc交叉编译环境搭建

    Ubuntu下arm-Linux-gcc交叉编译环境搭建 参考:http://blog.csdn.net/hebbely/article/details/53992805 1.网上下载 arm-lin ...

  4. OpenCV基于ARM的Linux系统的交叉编译

    OpenCV基于ARM的Linux系统的交叉编译 基于ARM的Linux系统的交叉编译 先决条件 获取OpenCV源代码 获取最新的稳定OpenCV版本 从Git存储库中获取最新的OpenCV 构建O ...

  5. ubuntu14.04安装arm-linux-gcc,Ubuntu14.04下arm-linux-gcc交叉编译环境搭建

    Ubuntu下arm-linux-gcc交叉编译环境搭建 系统:Ubuntu 14.04 32bit 1.网上下载arm-linux-gcc-4.4.3.tar.gz 2.解压 sudo tar -x ...

  6. Altera 的SOC器件之将自定义的IP挂在ARM硬核下(通过avalon总线),实现arm核与IP之间的通信

    Altera 的SOC器件之将自定义的IP挂在ARM硬核下(通过avalon总线),实现arm核与IP之间的通信 软件: Quartus II 17.0 芯片: ALTERA Cyclone5 5CG ...

  7. 银河麒麟V10(飞腾2000+ ARM)环境下构建达梦V8数据库容器镜像

    1.在银河麒麟V10(飞腾2000+ ARM)设备下,先下载一个镜像,我这里选择了centos: docker pull centos:centos7.9.2009 2.启动一个容器: sudo do ...

  8. Mac下ndk交叉编译arm平台程序

    1.下载ndk 2.使用ndk交叉编译exp.c <1>.编写:Makefile NDK_ROOT=/Users/xxx/Documents/Android_SDK/android-ndk ...

  9. 【OpenCV3】Opencv3.2.0在Hisi3521下的交叉编译和移植

    说明:hisi3521交叉编译工具arm-hisiv300-linux已经配置完成,cmake已经安装. 1.      从官网(https://github.com/opencv/opencv/ar ...

最新文章

  1. C语言 · 计算时间
  2. sap系统操作流程财务软件_金蝶财务软件的操作流程汇总
  3. [wp7游戏]wp7~~飞行射击精品游戏~~集合贴
  4. macos访问linux分区,在linux中访问macos 下的分区。
  5. 7. 关于IntelliJ IDEA删除项目
  6. 安装ipython失败 in error catcher_疯狂的Python:零基础小白入门帖子详情 - 网易云课堂...
  7. [Android]BaseExpandableListAdapter实现可折叠列表
  8. idea 全部报错找不到包
  9. 拒绝空谈 AI 设想!手把手教你构建实时、高可用的 AI 调度平台
  10. python函数参数生成器_python函数补充、生成器、迭代器
  11. win11系统下安装java 8的教程
  12. 三星s8 android9.0官方rom,三星s8刷机包(最新固件升级V9.0)
  13. echarts x轴 y轴设置
  14. python写excel文件头_Python帮你做Excel——写入Excel文档
  15. 老派道场普陀山 海天佛国的禅修氛围
  16. IBinder对象在进程间传递的形式(二)
  17. 几种线程安全的Map解析,真香系列
  18. jetson nx fan auto pwm
  19. html中注册商标r怎么打,如何在PPT里打一个圈加一个R,就是已经注册的标志?
  20. java微信公众号开发之各种事件推送

热门文章

  1. table标签的介绍
  2. PrometheusAlert安装和其基本的使用
  3. 英语和汉语语法方面的区别(语言类型学)
  4. BlueStacks不仅支持x86和ARM处理器,新版BlueStacks并不是单纯的实现虚拟化,而是运行整个Android系统
  5. Win11老是弹出输入体验怎么办
  6. android 微信分身开发,【技巧】2021安卓手机微信分身方法
  7. 平面度、 共面度、 翘曲度三者区别
  8. 信息安全专业要计算机好吗,哪些单位需要计算机信息安全专业的
  9. python猜字游戏算法设计_python入门到实践-猜字游戏
  10. Mac终端加入IDEA命令