您尚未登录。

楼主 # 2024-06-09 01:50:51

yorling
会员
注册时间: 2024-06-09
已发帖子: 3
积分: 18

lichee nano rust 交叉编译失败(依赖 libasound)

使用的官方的工具链:https://wiki.sipeed.com/hardware/zh/lichee/RV_Nano/6_develop_mainline.html

我的 `.cargo/config.toml`:
```
[target.riscv64gc-unknown-linux-gnu]
rustflags = [
    "-C", "link-arg=--sysroot=/home/yorling/licheenano-sysroot",
    "-C", "link-arg=-L/home/yorling/licheenano-sysroot/usr/lib/riscv64-linux-gnu",
    # "-C", "link-arg=-L/home/yorling/licheenano-sysroot/usr/lib/riscv64-linux-musl",
    # "-C", "link-arg=-L/home/yorling/licheenano-toolchain/gcc/riscv64-linux-musl-x86_64/sysroot/usr/lib64/lp64",
    "-C", "link-arg=-mcpu=c906fdv",
    "-C", "link-arg=-march=rv64imafdcv0p7xthead",
    "-C", "link-arg=-mcmodel=medany",
    "-C", "link-arg=-mabi=lp64d",
    "-C", "target-feature=+crt-static",
]
# linker = "/home/yorling/licheenano-toolchain/gcc/riscv64-linux-musl-x86_64/bin/riscv64-unknown-linux-musl-gcc"
linker = "/home/yorling/licheenano-toolchain/gcc/riscv64-linux-x86_64/bin/riscv64-unknown-linux-gnu-gcc"
```

其中 sysroot 是我从 nano 上面直接复制下来的:
```
rsync -avz --delete --progress --exclude={"/proc/*","/sys/*","/dev/*","/tmp/*","/run/*","/mnt/*","/media/*","/var/cache/apt/*"} usblicheenano:/ ~/licheenano-sysroot
```

依赖都已安装,并且手动 check 确实存在:
```
midi master* 7s
❯ ls /home/yorling/licheenano-sysroot/usr/lib/riscv64-linux-gnu/crt1.o
/home/yorling/licheenano-sysroot/usr/lib/riscv64-linux-gnu/crt1.o

midi master*
❯ ls /home/yorling/licheenano-sysroot/usr/lib/riscv64-linux-gnu/libasound.so
/home/yorling/licheenano-sysroot/usr/lib/riscv64-linux-gnu/libasound.so
```

编译都没问题,但是最后链接的时候报错:

```
= note: /home/yorling/licheenano-toolchain/gcc/riscv64-linux-x86_64/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: cannot find crt1.o: No such file or directory
          /home/yorling/licheenano-toolchain/gcc/riscv64-linux-x86_64/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: cannot find -lasound
          /home/yorling/licheenano-toolchain/gcc/riscv64-linux-x86_64/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: /home/yorling/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/riscv64gc-unknown-linux-gnu/lib/libstd-e6f743a77fe832a8.rlib(std-e6f743a77fe832a8.std.fcef57a55afbbf4d-cgu.0.rcgu.o): in function `std::sys::pal::unix::os::home_dir::fallback':
          /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys/pal/unix/os.rs:732: warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
          /home/yorling/licheenano-toolchain/gcc/riscv64-linux-x86_64/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: /home/yorling/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/riscv64gc-unknown-linux-gnu/lib/libstd-e6f743a77fe832a8.rlib(std-e6f743a77fe832a8.std.fcef57a55afbbf4d-cgu.0.rcgu.o): in function `<std::sys_common::net::LookupHost as core::convert::TryFrom<(&str,u16)>>::try_from::{{closure}}':
          /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/net.rs:207: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
          collect2: error: ld returned 1 exit status
```

有大佬知道是什么问题吗,折腾一天了,还是没有成功😭

核心报错是:
```
cannot find crt1.o: No such file or directory
cannot find -lasound
```

离线

#1 2024-06-09 12:22:00

海石生风
会员
所在地: 深圳
注册时间: 2019-07-02
已发帖子: 544
积分: 665
个人网站

Re: lichee nano rust 交叉编译失败(依赖 libasound)

你弄成静态链接编译了,但libasound.so是动态库肯定链接不上呀。要么提供静态库,要么改为动态链接编译。

离线

楼主 #2 2024-06-09 12:59:03

yorling
会员
注册时间: 2024-06-09
已发帖子: 3
积分: 18

Re: lichee nano rust 交叉编译失败(依赖 libasound)

海石生风 说:

你弄成静态链接编译了,但libasound.so是动态库肯定链接不上呀。要么提供静态库,要么改为动态链接编译。

换成动态编译也是一样的,不过报错变成了另一个报错,找不到 Scrt1.o,但是实际上这个文件是存在的

```
= note: /home/yorling/licheenano-toolchain/gcc/riscv64-linux-x86_64/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: cannot find Scrt1.o: No such file or directory
          collect2: error: ld returned 1 exit status


warning: `midi` (bin "midi") generated 15 warnings
error: could not compile `midi` (bin "midi") due to 1 previous error; 15 warnings emitted

midi master* 37s
❯ ls /home/yorling/licheenano-sysroot/usr/lib/riscv64-linux-gnu/Scrt1.o
/home/yorling/licheenano-sysroot/usr/lib/riscv64-linux-gnu/Scrt1.o
```

离线

楼主 #3 2024-06-09 13:17:48

yorling
会员
注册时间: 2024-06-09
已发帖子: 3
积分: 18

Re: lichee nano rust 交叉编译失败(依赖 libasound)

已解决!sysroot 不用从目标系统上复制,直接用官方提供的工具链里面的就行。

附上最后成功的配置:
```
[target.riscv64gc-unknown-linux-gnu]
rustflags = [
    "-C", "link-arg=--sysroot=/home/yorling/licheenano-toolchain/gcc/riscv64-linux-x86_64/sysroot",
    "-C", "link-arg=-L/home/yorling/alsa_static/lib",
    "-C", "link-arg=-mcpu=c906fdv",
    "-C", "link-arg=-march=rv64imafdcv0p7xthead",
    "-C", "link-arg=-mcmodel=medany",
    "-C", "link-arg=-mabi=lp64d",
    "-C", "target-feature=+crt-static",
]
linker = "/home/yorling/licheenano-toolchain/gcc/riscv64-linux-x86_64/bin/riscv64-unknown-linux-gnu-gcc"

```

另外 asla 官方不提供预编译的静态链接库,需要自己手动编译下:
```
export RISCV=/home/yorling/licheenano-toolchain/gcc/riscv64-linux-x86_64
export PATH=$RISCV/bin:$PATH
export CC=riscv64-unknown-linux-gnu-gcc
export CXX=riscv64-unknown-linux-gnu-g++
export AR=riscv64-unknown-linux-gnu-ar
export AS=riscv64-unknown-linux-gnu-as
export RANLIB=riscv64-unknown-linux-gnu-ranlib
export LD=riscv64-unknown-linux-gnu-ld


./configure --host=riscv64-unknown-linux-gnu --prefix=/home/yorling/alsa_static --enable-static --disable-shared
make
make install
```

不过似乎读取 alsa 配置(读取的还是编译机器上的路径)还有点问题,我再调试下。

感谢各位!

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn