簡體   English   中英

使用 go build CGO_ENABLED 交叉編譯 - 警告:未找到 libudev.so.1

[英]Cross-compile using go build CGO_ENABLED - warning: libudev.so.1 not found

由於libusb依賴性,我正在嘗試為CGO_ENABLED=1的 Raspberry Pi 交叉編譯 Golang 應用程序

在編譯期間,我收到錯誤:

arm-linux-gnueabihf/bin/ld: warning: libudev.so.1, needed by /usr/lib/arm-linux-gnueabihf/libusb-1.0.so, not found (try using -rpath or -rpath-link)

我嘗試了以下命令的多種變體,但沒有成功:

CGO_ENABLED=1 GOARCH=arm GOARM=7 PKG_CONFIG_LIBDIR=/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ go build -x -ldflags '-extld=arm-linux-gnueabihf-gcc -rpath=/usr/lib/arm-linux-gnueabihf -L/usr/lib/arm-linux-gnueabihf -ludev'

當我使用-x標志運行構建時,我得到了詳細的輸出,看起來它在構建src/net包時失敗了:

cd /usr/local/go/src/net
/usr/local/go/pkg/tool/linux_amd64/cgo -dynpackage net -dynimport $WORK/net/_obj/_cgo_.o -dynout $WORK/net/_obj/_cgo_import.go
arm-linux-gnueabihf-gcc -I . -fPIC -marm -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -o $WORK/net/_obj/_all.o $WORK/net/_obj/_cgo_export.o $WORK/net/_obj/cgo_linux.cgo2.o $WORK/net/_obj/cgo_resnew.cgo2.o $WORK/net/_obj/cgo_socknew.cgo2.o $WORK/net/_obj/cgo_unix.cgo2.o -g -O2 -Wl,-r -nostdlib -Wl,--build-id=none
/usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/net.a -trimpath $WORK -p net -buildid 9e58c94d1ddeba63666a35ecee9409056baf5d3c -D _/usr/local/go/src/net -I $WORK -pack ./addrselect.go ./conf.go ./dial.go ./dnsclient.go ./dnsclient_unix.go ./dnsconfig_unix.go ./dnsmsg.go ./fd_mutex.go ./fd_poll_runtime.go ./fd_posix.go ./fd_unix.go ./file.go ./file_unix.go ./hook.go ./hook_cloexec.go ./hook_unix.go ./hosts.go ./interface.go ./interface_linux.go ./ip.go ./iprawsock.go ./iprawsock_posix.go ./ipsock.go ./ipsock_posix.go ./lookup.go ./lookup_unix.go ./mac.go ./net.go ./nss.go ./parse.go ./pipe.go ./port.go ./port_unix.go ./sendfile_linux.go ./sock_cloexec.go ./sock_linux.go ./sock_posix.go ./sockopt_linux.go ./sockopt_posix.go ./sockoptip_linux.go ./sockoptip_posix.go ./tcpsock.go ./tcpsock_posix.go ./tcpsockopt_posix.go ./tcpsockopt_unix.go ./udpsock.go ./udpsock_posix.go ./unixsock.go ./unixsock_posix.go ./writev_unix.go $WORK/net/_obj/_cgo_gotypes.go $WORK/net/_obj/cgo_linux.cgo1.go $WORK/net/_obj/cgo_resnew.cgo1.go $WORK/net/_obj/cgo_socknew.cgo1.go $WORK/net/_obj/cgo_unix.cgo1.go $WORK/net/_obj/_cgo_import.go
# github.com/resin-io/edge-node-manager/vendor/github.com/kylelemons/gousb/usb
/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/../lib/gcc/arm-linux-gnueabihf/4.8.3/../../../../arm-linux-gnueabihf/bin/ld: warning: libudev.so.1, needed by /usr/lib/arm-linux-gnueabihf/libusb-1.0.so, not found (try using -rpath or -rpath-link)
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_monitor_filter_add_match_subsystem_devtype@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_monitor_enable_receiving@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_enumerate_scan_devices@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_new@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_device_get_devnode@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_enumerate_new@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_list_entry_get_name@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_enumerate_add_match_subsystem@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_device_get_sysname@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_enumerate_get_list_entry@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_device_new_from_syspath@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_monitor_new_from_netlink@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_monitor_receive_device@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_device_unref@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_enumerate_unref@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_monitor_get_fd@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_list_entry_get_next@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_monitor_unref@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `clock_gettime@GLIBC_2.17'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_unref@LIBUDEV_183'
/usr/lib/arm-linux-gnueabihf/libusb-1.0.so: undefined reference to `udev_device_get_action@LIBUDEV_183'
collect2: error: ld returned 1 exit status

奇怪的是,編譯器可以找到/usr/lib/arm-linux-gnueabihf/libusb-1.0.so並且如果我在該目錄中執行ls -allibudev.so也存在。 兩個文件都鏈接回/lib/arm-linux-gnueabihf/

lrwxrwxrwx 1 root root     41 Jan  7 04:14 libudev.so -> /lib/arm-linux-gnueabihf/libudev.so.1.5.0
-rw-r--r-- 1 root root 105464 Jun 16  2014 libusb-1.0.a
lrwxrwxrwx 1 root root     44 Jun 16  2014 libusb-1.0.so -> /lib/arm-linux-gnueabihf/libusb-1.0.so.0.1.0
drwxr-xr-x 2 root root   4096 Mar 15 14:48 pkgconfig

/usr/lib/arm-linux-gnueabihf/pkgconfig包含:

-rw-r--r-- 1 root root  513 Jan  7 04:13 libudev.pc
-rw-r--r-- 1 root root  332 Jun 16  2014 libusb-1.0.pc

最后,運行file顯示libusblibudev都是正確的架構:

/lib/arm-linux-gnueabihf/libusb-1.0.so.0.1.0: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=7d7b487c91042c1f254b39516b4aeabedfb828fc, stripped
/lib/arm-linux-gnueabihf/libudev.so.1.5.0: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=334f4f0d8b1222a324ccd0827c1613fdd1e2ec15, stripped

is error. 路徑錯誤。

它應該在以下目錄之一中:

./arm-linux-gnueabihf/lib
./arm-linux-gnueabihf/libc/lib
./arm-linux-gnueabihf/libc/usr/lib

但是很抱歉我忘記了哪個目錄是正確的。 to all of these directories.因此,您可以嘗試將復制到所有這些目錄中。

我知道這是一篇舊帖子,但我在 Rust 上遇到了類似的問題,因此我決定將我的解決方法作為備忘錄發布。

tl;博士

使用-rpath-link告訴ld一個正確的路徑。 對於 Rust,請使用RUSTFLAGS例如:

RUSTFLAGS="-C link-arg=-Wl,-rpath-link,/lib/arm-linux-gnueabihf"

投機

似乎/usr/lib/arm-linux-gnueabihf是通過libusb-1.0.pc libdir指定的,例如:

prefix=/usr
exec_prefix=${prefix}
libdir=${prefix}/lib/arm-linux-gnueabihf
includedir=${prefix}/include

Name: libusb-1.0
Description: C API for USB device access from Linux, Mac OS X, Windows, OpenBSD/NetBSD and Solaris userspace
Version: 1.0.21
Libs: -L${libdir} -lusb-1.0
Libs.private: -ludev  -pthread
Cflags: -I${includedir}/libusb-1.0

但是/lib/arm-linux-gnueabihf是通過libudev.pc libdir指定的,例如:

#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

prefix=/usr
exec_prefix=/usr
libdir=/lib/arm-linux-gnueabihf
includedir=/usr/include

Name: libudev
Description: Library to access udev device information
Version: 237
Libs: -L${libdir} -ludev
Cflags: -I${includedir}

所以我試過了

  • 創建從/lib/arm-linux-gnueabihf/libudev.so.1/usr/lib/arm-linux-gnueabihf/libudev.so.1的符號鏈接
  • /lib/arm-linux-gnueabihfLD_LIBRARY_PATH

但只有-rpath-link有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM