为什么赢了';我的交叉编译CGO二进制文件是否在Raspberry Pi(Raspbian)上运行?

为什么赢了';我的交叉编译CGO二进制文件是否在Raspberry Pi(Raspbian)上运行?,go,bluetooth,raspberry-pi,raspbian,cgo,Go,Bluetooth,Raspberry Pi,Raspbian,Cgo,当我编译以下代码片段()时: 我得到一个1.6Mb的二进制文件: 适用于我的ODROID-C1(ARMv7,running Arch,来自archlinuxarm.org),但是 不适用于我的Raspberry Pi B+(ARMv6,运行Raspbian,来自raspberrypi.org) 当我在Pi上运行它时,我得到一个“分段错误” 当我使用这些环境变量运行它时: CC=arm-linux-gnueabihf-gcc CGO_ENABLED=1 GOARCH=arm GOARM=6

当我编译以下代码片段()时:

我得到一个1.6Mb的二进制文件:

  • 适用于我的ODROID-C1(ARMv7,running Arch,来自archlinuxarm.org),但是
  • 不适用于我的Raspberry Pi B+(ARMv6,运行Raspbian,来自raspberrypi.org)
当我在Pi上运行它时,我得到一个“
分段错误

当我使用这些环境变量运行它时:

CC=arm-linux-gnueabihf-gcc
CGO_ENABLED=1
GOARCH=arm
GOARM=6
LD_PRELOAD=/lib/arm-linux-gnueabihf/libSegFault.so
SEGFAULT_USE_ALTSTACK=1
我得到这个输出:

*** Segmentation fault
Register dump:
 R0: 00000000   R1: 00000001   R2: bebf37f4   R3: bebf37fc
 R4: 00000000   R5: 00000000   R6: 00010c59   R7: 00000000
 R8: 00000000   R9: 00000000   SL: b6f33000   FP: 00000000
 IP: 0000004f   SP: bebf37ec   LR: 00010c6b   PC: 0005d7f4
 CPSR: 40000030
 Trap: 0000000e   Error: 00000817   OldMask: 00000000
 Addr: 00000001
Backtrace:
Memory map:
00010000-00125000 r-xp 00000000 b3:02 16193      /home/pi/test
00135000-0013b000 rw-p 00115000 b3:02 16193      /home/pi/test
0013b000-0014a000 rw-p 00000000 00:00 0 
0026b000-00290000 rw-p 00000000 00:00 0          [heap]
b6d53000-b6d73000 r-xp 00000000 b3:02 16080      /lib/arm-linux-gnueabihf/libgcc_s.so.1
b6d73000-b6d7a000 ---p 00020000 b3:02 16080      /lib/arm-linux-gnueabihf/libgcc_s.so.1
b6d7a000-b6d7b000 rw-p 0001f000 b3:02 16080      /lib/arm-linux-gnueabihf/libgcc_s.so.1
b6d7b000-b6e9f000 r-xp 00000000 b3:02 4254       /lib/arm-linux-gnueabihf/libc-2.13.so
b6e9f000-b6ea6000 ---p 00124000 b3:02 4254       /lib/arm-linux-gnueabihf/libc-2.13.so
b6ea6000-b6ea8000 r--p 00123000 b3:02 4254       /lib/arm-linux-gnueabihf/libc-2.13.so
b6ea8000-b6ea9000 rw-p 00125000 b3:02 4254       /lib/arm-linux-gnueabihf/libc-2.13.so
b6ea9000-b6eac000 rw-p 00000000 00:00 0 
b6eac000-b6ec0000 r-xp 00000000 b3:02 4247       /lib/arm-linux-gnueabihf/libpthread-2.13.so
b6ec0000-b6ec7000 ---p 00014000 b3:02 4247       /lib/arm-linux-gnueabihf/libpthread-2.13.so
b6ec7000-b6ec8000 r--p 00013000 b3:02 4247       /lib/arm-linux-gnueabihf/libpthread-2.13.so
b6ec8000-b6ec9000 rw-p 00014000 b3:02 4247       /lib/arm-linux-gnueabihf/libpthread-2.13.so
b6ec9000-b6ecb000 rw-p 00000000 00:00 0 
b6ecb000-b6ee0000 r-xp 00000000 b3:02 44311      /usr/lib/arm-linux-gnueabihf/libbluetooth.so.3.12.0
b6ee0000-b6ee7000 ---p 00015000 b3:02 44311      /usr/lib/arm-linux-gnueabihf/libbluetooth.so.3.12.0
b6ee7000-b6ee8000 r--p 00014000 b3:02 44311      /usr/lib/arm-linux-gnueabihf/libbluetooth.so.3.12.0
b6ee8000-b6eea000 rw-p 00015000 b3:02 44311      /usr/lib/arm-linux-gnueabihf/libbluetooth.so.3.12.0
b6ef8000-b6efa000 r-xp 00000000 b3:02 25761      /usr/lib/arm-linux-gnueabihf/libcofi_rpi.so
b6efa000-b6f01000 ---p 00002000 b3:02 25761      /usr/lib/arm-linux-gnueabihf/libcofi_rpi.so
b6f01000-b6f02000 rw-p 00001000 b3:02 25761      /usr/lib/arm-linux-gnueabihf/libcofi_rpi.so
b6f02000-b6f04000 r-xp 00000000 b3:02 4246       /lib/arm-linux-gnueabihf/libSegFault.so
b6f04000-b6f0b000 ---p 00002000 b3:02 4246       /lib/arm-linux-gnueabihf/libSegFault.so
b6f0b000-b6f0c000 r--p 00001000 b3:02 4246       /lib/arm-linux-gnueabihf/libSegFault.so
b6f0c000-b6f0d000 rw-p 00002000 b3:02 4246       /lib/arm-linux-gnueabihf/libSegFault.so
b6f0d000-b6f2a000 r-xp 00000000 b3:02 4248       /lib/arm-linux-gnueabihf/ld-2.13.so
b6f2e000-b6f32000 rw-p 00000000 00:00 0 
b6f32000-b6f33000 r--p 0001d000 b3:02 4248       /lib/arm-linux-gnueabihf/ld-2.13.so
b6f33000-b6f34000 rw-p 0001e000 b3:02 4248       /lib/arm-linux-gnueabihf/ld-2.13.so
bebd3000-bebf4000 rw-p 00000000 00:00 0 
bee7f000-bee80000 r-xp 00000000 00:00 0          [sigpage]
ffff0000-ffff1000 r-xp 00000000 00:00 0          [vectors]
Segmentation fault
/etc/ld.so.preload
中删除
/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so
会提供相同的输出,除了没有包含
libcofi_rpi.so的行之外

尝试使用
-tags netgo-a-ldflags“-linkmode external-extldflags-static”
构建静态链接的二进制文件时,会给我一条“
非法指令”
”而不是“
分段错误”


尝试在Pi本身上构建二进制代码是可行的,但是,我想交叉编译它。交叉编译确实有效,但只适用于ODROID-C1/Arch,而不适用于Raspberry Pi/Raspbian

我使用的确切构建步骤可以使用Docker复制:

$ docker run -it golang:1.4.1-cross bash
在docker内部,我运行:

echo“debhttp://emdebian.org/tools/debian/ jessie main“>/etc/apt/sources.list.d/crosstools.list
curl-shttp://emdebian.org/tools/debian/emdebian-toolchain-archive.key |apt密钥添加-
dpkg—添加架构armhf
更新源
apt get安装-y crossbuild essential armhf libbluetooth dev:armhf
export CC=arm linux gnueabihf gcc
导出CGO_已启用=1
导出目标=arm
出口目标=6
go build-o test test.go
这给了我一个名为“test”的可执行文件,如上所述,它不会在Pi上运行

以下是有关我的测试设备的一些信息:

树莓皮B+: 已安装的蓝牙库:

$ dpkg -s libbluetooth3

Package: libbluetooth3
Status: install ok installed
Priority: optional
Section: libs
Installed-Size: 172
Maintainer: Debian Bluetooth Maintainers <pkg-bluetooth-maintainers@lists.alioth.debian.org>
Architecture: armhf
Multi-Arch: same
Source: bluez
Version: 4.99-2
Replaces: libsdp2 (<= 1.5-2)
Depends: libc6 (>= 2.13-28)
Pre-Depends: multiarch-support
Conflicts: libsdp2 (<= 1.5-2)
Description: Library to use the BlueZ Linux Bluetooth stack
 BlueZ is the official Linux Bluetooth protocol stack. It is an Open Source
 project distributed under GNU General Public License (GPL).
Homepage: http://www.bluez.org
$ pacman -Qe bluez
bluez 5.27-1
$ dpkg -s libbluetooth-dev:armhf
Package: libbluetooth-dev
Status: install ok installed
Priority: extra
Section: libdevel
Installed-Size: 429
Maintainer: Debian Bluetooth Maintainers <pkg-bluetooth-maintainers@lists.alioth.debian.org>
Architecture: armhf
Source: bluez (5.23-2)
Version: 5.23-2+b1
Replaces: libsdp2-dev (<= 1.5.2)
Provides: libbluetooth3-dev
Depends: libbluetooth3 (= 5.23-2+b1), libc6-dev | libc-dev
Suggests: pkg-config
Conflicts: bluez-pan, libbluetooth-dev, libsdp2-dev (<= 1.5.2)
Description: Development files for using the BlueZ Linux Bluetooth library
 BlueZ is the official Linux Bluetooth protocol stack. It is an Open Source
 project distributed under GNU General Public License (GPL).
Homepage: http://www.bluez.org
已安装的蓝牙库和开发头文件:

$ dpkg -s libbluetooth3

Package: libbluetooth3
Status: install ok installed
Priority: optional
Section: libs
Installed-Size: 172
Maintainer: Debian Bluetooth Maintainers <pkg-bluetooth-maintainers@lists.alioth.debian.org>
Architecture: armhf
Multi-Arch: same
Source: bluez
Version: 4.99-2
Replaces: libsdp2 (<= 1.5-2)
Depends: libc6 (>= 2.13-28)
Pre-Depends: multiarch-support
Conflicts: libsdp2 (<= 1.5-2)
Description: Library to use the BlueZ Linux Bluetooth stack
 BlueZ is the official Linux Bluetooth protocol stack. It is an Open Source
 project distributed under GNU General Public License (GPL).
Homepage: http://www.bluez.org
$ pacman -Qe bluez
bluez 5.27-1
$ dpkg -s libbluetooth-dev:armhf
Package: libbluetooth-dev
Status: install ok installed
Priority: extra
Section: libdevel
Installed-Size: 429
Maintainer: Debian Bluetooth Maintainers <pkg-bluetooth-maintainers@lists.alioth.debian.org>
Architecture: armhf
Source: bluez (5.23-2)
Version: 5.23-2+b1
Replaces: libsdp2-dev (<= 1.5.2)
Provides: libbluetooth3-dev
Depends: libbluetooth3 (= 5.23-2+b1), libc6-dev | libc-dev
Suggests: pkg-config
Conflicts: bluez-pan, libbluetooth-dev, libsdp2-dev (<= 1.5.2)
Description: Development files for using the BlueZ Linux Bluetooth library
 BlueZ is the official Linux Bluetooth protocol stack. It is an Open Source
 project distributed under GNU General Public License (GPL).
Homepage: http://www.bluez.org
Docker构建环境 已安装的蓝牙开发标题:

$ dpkg -s libbluetooth3

Package: libbluetooth3
Status: install ok installed
Priority: optional
Section: libs
Installed-Size: 172
Maintainer: Debian Bluetooth Maintainers <pkg-bluetooth-maintainers@lists.alioth.debian.org>
Architecture: armhf
Multi-Arch: same
Source: bluez
Version: 4.99-2
Replaces: libsdp2 (<= 1.5-2)
Depends: libc6 (>= 2.13-28)
Pre-Depends: multiarch-support
Conflicts: libsdp2 (<= 1.5-2)
Description: Library to use the BlueZ Linux Bluetooth stack
 BlueZ is the official Linux Bluetooth protocol stack. It is an Open Source
 project distributed under GNU General Public License (GPL).
Homepage: http://www.bluez.org
$ pacman -Qe bluez
bluez 5.27-1
$ dpkg -s libbluetooth-dev:armhf
Package: libbluetooth-dev
Status: install ok installed
Priority: extra
Section: libdevel
Installed-Size: 429
Maintainer: Debian Bluetooth Maintainers <pkg-bluetooth-maintainers@lists.alioth.debian.org>
Architecture: armhf
Source: bluez (5.23-2)
Version: 5.23-2+b1
Replaces: libsdp2-dev (<= 1.5.2)
Provides: libbluetooth3-dev
Depends: libbluetooth3 (= 5.23-2+b1), libc6-dev | libc-dev
Suggests: pkg-config
Conflicts: bluez-pan, libbluetooth-dev, libsdp2-dev (<= 1.5.2)
Description: Development files for using the BlueZ Linux Bluetooth library
 BlueZ is the official Linux Bluetooth protocol stack. It is an Open Source
 project distributed under GNU General Public License (GPL).
Homepage: http://www.bluez.org

更新: 在Pi上尝试使用Arch时,交叉编译的二进制文件也会SEGFULTS,给出与
GOARM=5
6
7
相同的消息(
非法指令(内核转储)

到目前为止,我还没有找到一种方法来交叉编译Pi的CGO代码,它与
libbluetooth
相链接


明天我将尝试覆盆子皮2。

因此,在经过数周的反复试验后,让我通过总结来回答我自己的问题

…它就是不起作用。 作为最后手段,我尝试使用与编译上述代码相同的Raspbian图像,并使用运行它。你猜怎么着:我犯了个错误。同样的指令,使用同样的图像,当我在Pi上运行它时,会给我一个工作的可执行文件,当我模拟Pi或试图以任何方式交叉编译它时,会给我一个segfault


最后,这不值得这么麻烦。我们刚刚建立了专门的Pi来构建我们的项目,“生产”Pi只需提取更新后的二进制文件。

您是否尝试在raspberry Pi中编译?“尝试在Pi本身上构建二进制文件是可行的,但是,我想交叉编译它。”您有过这样的工作吗?我目前的情况是,交叉编译会使事情变得非常容易,因为我试图构建的go项目对于Pi的RAM来说显然太多了。我使用的工具链和环境变量与您几乎相同,而且也会出现分段错误。我最后的办法是增加Pi上的掉期规模,但如果可能的话,我希望避免这种情况。谢谢不,不幸的是,我没有让它工作。最后,我切换到了ARMv7版本,这似乎是可行的。我使用了ODROID-C,但Pi 2也应该可以工作。太糟糕了,我还得在设备上为我的旧Pi B+编译。这太糟糕了。我想我会再次尝试在我的Pi B+上编译它,并增加交换大小。谢谢你的回复!