我无处可问,每件事都是如此具体,搜索和谷歌不起作用。花了几十个小时在这上面。
我正在为我的LinkSys MR8300 V1.1路由器构建一个Go程序,它有armv7l处理器(uname -a输出),即ARM v7,32位CPU。我在路由器上安装了OpenWrt操作系统。
该应用程序的想法是连接USB硬币接收器,并启用互联网时,你丢硬币30分钟。我在Ubuntu桌面上有一个工作应用程序,它可以理解设备,在桌面上一切都很好。
然而,路由器是32位,ARM,所以事情有点不同,更复杂。
我的路由器上的cat /proc/cpuinfo提供了如下内容:
processor : 0
model name : ARMv7 Processor rev 5 (v7l)
BogoMIPS : 26.81
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc07
CPU revision : 5我使用交叉编译,因为路由器只有19 of的磁盘空间(并不是所有的开发包都可用)。
对于交叉编译,我使用的是x86_64 Ubuntu桌面和OpenWrt提供的工具链。当您需要配置和制作工具链时,我已经过了这个阶段。我能够在我的路由器上编译和执行基本C程序,例如:
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}甚至是Go项目,比如:
package main
func main() {
println("Coin acceptor device control program")
}当我将二进制文件上传到路由器时,没有任何问题。
当我试图在路由器内部使用USB库时,问题就开始了。因为它是交叉编译的,所以我应该提到我拥有的环境变量:
STAGING_DIR=/home/ro/work/openwrt/staging_dir
TOOLCHAIN_DIR=/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi
LDCFLAGS=/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
LD_LIBRARY_PATH=/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
LDFLAGS=-L/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
CGO_LDFLAGS=-L/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
CFLAGS=-I/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/include
CC=arm-openwrt-linux-gcc
GOARCH=arm
CROSS_COMPILE=arm-openwrt-linux-gcc
CGO_ENABLED=1
GOOS=linux
GOARM=7对于这些环境变量,go get github.com/karalabe/hid命令按预期工作(我的/home/ro/go/pkg/linux_arm/github.com/karalabe文件夹中有hid.a二进制文件)。
这个url https://github.com/karalabe/hid是我为USB使用的库,标题是"Gopher接口设备(USB )“。但是,它依赖于某些C库..。所以我想这就是GOC发挥作用的地方。图书馆说:
将这两个依赖项直接分发到存储库中,并使用CGO包装,从而使hid包自成体系,并可获取。
正如我前面提到的,我可以在我的桌面上编译简单的应用程序并将它们上传到路由器,这些应用程序可以工作!但是,当我尝试使用这个库时,脚本就有点不对劲了。
这是我的应用程序:
main.go
package main
import (
"fmt"
)
func main() {
println("Hello")
devices := FindAll(0x0079)
if len(devices) == 0 {
fmt.Println("No coin acceptor found")
return
}
println("Found coin acceptor")
}device.go
package main
import (
"errors"
"github.com/karalabe/hid"
)
type Device struct {
info hid.DeviceInfo
dev *hid.Device
Name string
PID uint16
}
func (device *Device) Open() error {
if device.dev != nil {
return errors.New("device: Device handle is not null, but Open() called")
}
var err error
device.dev, err = device.info.Open()
return err
}
func (device *Device) Close() error {
if device.dev == nil {
return errors.New("device: Close() called for Device with null handle")
}
err := device.dev.Close()
device.dev = nil
return err
}
// Find all products with the given product ID.
func FindAll(product uint16) []Device {
deviceInfo := hid.Enumerate(0x1e7d, product)
if len(deviceInfo) == 0 {
return []Device{}
}
devices := make([]Device, len(deviceInfo))
for i, info := range deviceInfo {
devices[i] = Device{
info: info,
Name: info.Product,
PID: product,
}
}
return devices
}命令我用来构建一个东西:
go build ./main.go ./device.go二进制文件会产生,并且看起来很正常。直到我上传和运行路由器上的二进制文件。它大致上说:
Error relocating ./main: __pthread_cond_timedwait_time64: symbol not found
Error relocating ./main: __nanosleep_time64: symbol not found
Error relocating ./main: __stat_time64: symbol not found
Error relocating ./main: __clock_gettime64: symbol not found就是这样。甚至连“你好”都不印。
需要注意的一点是,由于某种原因,它引用(并抱怨)64位函数。因此,总括而言:
这是32位的ARM处理器。functions.
本身。
我想知道我错过了什么?我应该向编译器指定哪些标志,这样才不会使用64位函数?也许这些USB库里面有用C写的东西?我几乎没有任何想法,在这一点上,因为我不能使用我的USB设备与嵌入式OpenWrt设备。
objdump -p main输出:
objdump -p main
main: file format elf32-little
Program Header:
PHDR off 0x00000034 vaddr 0x00010034 paddr 0x00010034 align 2**2
filesz 0x00000120 memsz 0x00000120 flags r--
INTERP off 0x00000154 vaddr 0x00010154 paddr 0x00010154 align 2**0
filesz 0x00000018 memsz 0x00000018 flags r--
LOAD off 0x00000000 vaddr 0x00010000 paddr 0x00010000 align 2**16
filesz 0x001164ec memsz 0x001164ec flags r-x
LOAD off 0x00116d6c vaddr 0x00136d6c paddr 0x00136d6c align 2**16
filesz 0x00013cc4 memsz 0x00029c94 flags rw-
DYNAMIC off 0x00116f08 vaddr 0x00136f08 paddr 0x00136f08 align 2**2
filesz 0x000000f8 memsz 0x000000f8 flags rw-
NOTE off 0x0000016c vaddr 0x0001016c paddr 0x0001016c align 2**2
filesz 0x00000064 memsz 0x00000064 flags r--
TLS off 0x00116d6c vaddr 0x00136d6c paddr 0x00136d6c align 2**2
filesz 0x00000000 memsz 0x00000008 flags r--
STACK off 0x00000000 vaddr 0x00000000 paddr 0x00000000 align 2**4
filesz 0x00000000 memsz 0x00000000 flags rw-
RELRO off 0x00116d6c vaddr 0x00136d6c paddr 0x00136d6c align 2**0
filesz 0x00000294 memsz 0x00000294 flags r--
Dynamic Section:
NEEDED libgcc_s.so.1
NEEDED libc.so
INIT 0x0001437c
FINI 0x000a7abc
INIT_ARRAY 0x00136d6c
INIT_ARRAYSZ 0x00000004
FINI_ARRAY 0x00136d70
FINI_ARRAYSZ 0x00000004
HASH 0x000101d0
GNU_HASH 0x00010acc
STRTAB 0x00012820
SYMTAB 0x000114c0
STRSZ 0x00001568
SYMENT 0x00000010
DEBUG 0x00000000
PLTGOT 0x00137000
PLTRELSZ 0x00000360
PLTREL 0x00000011
JMPREL 0x0001401c
REL 0x00014014
RELSZ 0x00000008
RELENT 0x00000008
VERNEED 0x00013ff4
VERNEEDNUM 0x00000001
VERSYM 0x00013d88
Version References:
required from libgcc_s.so.1:
0x0b792655 0x00 02 GCC_3.5file main输出:
main: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-armhf.so.1, Go BuildID=VEoRmm2_70EkDLdXjTm0/APdquCT6lGlorzOlV3Un/jnQVIeEEaRXWGrmNQ-mE/Bjx-ldvZODg_GFcJuIgW, not stripped发布于 2022-07-19 22:24:05
终于来了!
我试图重新构建OpenWrt工具链,但这一次我切换到了一个类似于路由器openwrt-21.02的分支,make menuconfig和make又开始了。它本身并没有帮助,但我以这种方式稍微修改了env变量:
export STAGING_DIR=/home/ro/work/openwrt/staging_dir
export TOOLCHAIN_DIR=$STAGING_DIR/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi
export LDCFLAGS=$TOOLCHAIN_DIR/lib
export LD_LIBRARY_PATH=$TOOLCHAIN_DIR/lib
export LDFLAGS="-L${TOOLCHAIN_DIR}/lib"
export CGO_LDFLAGS="-Xlinker -rpath=${TOOLCHAIN_DIR}/lib/libc.so -static"
export CFLAGS="-I${TOOLCHAIN_DIR}/include"
export CC=arm-openwrt-linux-gcc-11.3.0
export PATH=$TOOLCHAIN_DIR/bin:$PATH
export GOARCH=arm
export CROSS_COMPILE=arm-openwrt-linux-gcc
export CGO_ENABLED=1
export GOOS=linux
export GOARM=7CGO_LDFLAGS指定到libc的静态链接。最后,所有的工作,我可以访问USB设备从嵌入式设备!
https://stackoverflow.com/questions/73042358
复制相似问题