当我编译以下代码片段 (playground link) 时:
package main
/*
#cgo LDFLAGS: -lbluetooth
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
*/
import "C"
func main() {
C.hci_get_route(nil)
}
…使用这些环境变量:
CC=arm-linux-gnueabihf-gcc
CGO_ENABLED=1
GOARCH=arm
GOARM=6
我得到一个 1.6Mb 的二进制文件:
当我在 Pi 上运行它时,出现“Segmentation fault”。
当我使用这些环境变量运行它时:
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" 构建一个静态链接的二进制文件给我一个 "Illegal instruction"而不是“段错误”。
尝试在 Pi 本身上构建二进制文件是可行的,但是,我想交叉编译它。交叉编译确实有效,但仅适用于 ODROID-C1/Arch,不适用于 Raspberry Pi/Raspbian。
我使用的确切构建步骤可以使用 Docker 重现:
$ docker run -it golang:1.4.1-cross bash
在 docker 中,我运行:
echo "deb http://emdebian.org/tools/debian/ jessie main" >/etc/apt/sources.list.d/crosstools.list
curl -s http://emdebian.org/tools/debian/emdebian-toolchain-archive.key | apt-key add -
dpkg --add-architecture armhf
apt-get update
apt-get install -y crossbuild-essential-armhf libbluetooth-dev:armhf
export CC=arm-linux-gnueabihf-gcc
export CGO_ENABLED=1
export GOARCH=arm
export GOARM=6
go build -o test test.go
这给了我一个名为“test”的可执行文件,如上所述,它不会在 Pi 上运行。
这是关于我的测试设备的一些信息:
$ uname -a
Linux raspberrypi 3.18.5+ #744 PREEMPT Fri Jan 30 18:19:07 GMT 2015 armv6l GNU/Linux
$ cat /proc/cpuinfo
processor : 0
model name : ARMv6-compatible processor rev 7 (v6l)
BogoMIPS : 2.00
Features : half thumb fastmult vfp edsp java tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xb76
CPU revision : 7
Hardware : BCM2708
Revision : 0010
Serial : 000000002a69d702
$ ldd test
/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0xb6f01000)
libbluetooth.so.3 => /usr/lib/arm-linux-gnueabihf/libbluetooth.so.3 (0xb6ed4000)
libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0xb6eb5000)
libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6d84000)
/lib/ld-linux-armhf.so.3 (0xb6f0e000)
已安装的蓝牙库:
$ 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
$ uname -a
Linux alarm 3.10.66-1-ARCH #1 SMP PREEMPT Wed Jan 28 18:12:35 MST 2015 armv7l GNU/Linux
$ cat /proc/cpuinfo
Processor : ARMv7 Processor rev 1 (v7l)
processor : 0
BogoMIPS : 3.27
processor : 1
BogoMIPS : 3.27
processor : 2
BogoMIPS : 3.27
processor : 3
BogoMIPS : 3.27
Features : swp half thumb fastmult vfp edsp neon vfpv3 tls vfpv4
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc05
CPU revision : 1
Hardware : ODROIDC
Revision : 000a
Serial : 1b00000000000000
$ ldd test
libbluetooth.so.3 => /usr/lib/libbluetooth.so.3 (0xb6eaf000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0xb6e87000)
libc.so.6 => /usr/lib/libc.so.6 (0xb6d48000)
/lib/ld-linux-armhf.so.3 (0xb6ed1000)
已安装的蓝牙库和开发头文件:
$ pacman -Qe bluez
bluez 5.27-1
$ uname -a
Linux 094aa2ed34a5 3.18.5-gentoo-tp #1 SMP Fri Feb 6 20:14:55 CET 2015 x86_64 GNU/Linux
已安装的蓝牙开发 header :
$ 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 容器中安装与 Pi 上可用版本完全相同的 libbluetooth-dev 没有区别。
将较新的 libbluetooth.so 从 Jessie 复制到 Wheezie 给我这个错误:
./test: /lib/arm-linux-gnueabihf/libc.so.6: version `GLIBC_2.15' not found (required by /home/pi/libbluetooth.so.3.17.11)
尝试在 Pi 上使用 Arch,交叉编译的二进制文件也出现段错误,使用 GOARM=5 给出相同的消息(“Illegal instruction (core dumped)”), 6 或 7。
到目前为止,我还没有找到一种为 Pi 交叉编译 CGO 代码的方法,链接到 libbluetooth。
明天将尝试使用 Raspberry Pi 2。
最佳答案
因此,在经过数周的反复试验之后,让我得出结论来回答我自己的问题,即......
作为最后的手段,我尝试使用与编译上述代码相同的 Raspbian 镜像,并使用QEMU 运行它。你猜怎么着:我遇到了段错误。完全相同的指令,使用完全相同的图像,当我在 Pi 上运行它时给我一个工作的可执行文件,当我模拟 Pi 或尝试以任何方式交叉编译它时给我一个段错误。
最后,这不值得麻烦。我们只是设置了专用的 Pi 来构建我们的项目,而“生产”Pi 只是拉取更新的二进制文件。
关于go - 为什么我的交叉编译的 CGO 二进制文件不能在 Raspberry Pi (Raspbian) 上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28416900/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R