登录 立即注册

首页 > 绿虎论坛 > 电脑 > 鸿蒙PC (发帖)

标题: 在鸿蒙PC上安装Rust编译器(rustc/cargo)的方法

作者: @Ta

时间: 6小时前发布,6小时前修改

点击: 39

操作步骤

  1. 打开开发者模式(提供HiShell终端)。

  2. 从应用商店安装Python安装器(提供python3命令,我们要用它安装SSL证书):

  3. 从应用商店安装DevBox(提供clang、ld和binary-sign-tool等命令,之后需要用到):

  4. 设置环境变量:

export CC=clang CXX=clang++
export PATH=~/.local/bin:$PATH
export LD_LIBRARY_PATH=~/.local/lib/aarch64-linux-ohos:~/.local/lib:/data/service/hnp/ohos-sdk.org/ohos-sdk_26.0.0.18/ohos/native/sysroot/usr/lib/aarch64-linux-ohos/:$LD_LIBRARY_PATH

把环境变量加入 ~/.zshrc 让其永久生效:

echo 'export CC=clang CXX=clang++' >> ~/.zshrc
echo 'export PATH=~/.local/bin:$PATH' >> ~/.zshrc
echo 'export LD_LIBRARY_PATH=~/.local/lib/aarch64-linux-ohos:~/.local/lib:/data/service/hnp/ohos-sdk.org/ohos-sdk_26.0.0.18/ohos/native/sysroot/usr/lib/aarch64-linux-ohos/:$LD_LIBRARY_PATH' >> ~/.zshrc
  1. 下载 arm64 ohos 版 rust 并签名安装:
# 下载
wget -O rust-1.96.0-aarch64-unknown-linux-ohos.tar.xz https://static.rust-lang.org/dist/rust-1.96.0-aarch64-unknown-linux-ohos.tar.xz

# 解压
tar vxf rust-1.96.0-aarch64-unknown-linux-ohos.tar.xz

# 进入解压出来的目录
cd rust-1.96.0-aarch64-unknown-linux-ohos

# 签名当前目录及其子目录下的所有ELF文件(Termony用户请注意:while循环与fish不兼容,请在zsh或bash中执行)
find . -type f -exec file '{}' ';' | grep --line-buffered ': ELF ' | awk -F': ELF ' '{print $1}' | while read f; do echo "$f"; binary-sign-tool sign -inFile "$f" -outFile "$f" -selfSign 1; done

# 安装
./install.sh --prefix=~/.local

# 测试 rustc 和 cargo 是否可用
rustc
cargo

你会发现 rustc 能用,但 cargo 缺少 libssl.so 和 libcrypto.so 库。

localhost ~ % rustc
Usage: rustc [OPTIONS] INPUT
localhost ~ % cargo
Error loading shared library libssl.so: (needed by /storage/Users/currentUser/.local/bin/cargo)
Error loading shared library libcrypto.so: (needed by /storage/Users/currentUser/.local/bin/cargo)
  1. 安装 libssl 库:
# 下载
wget -O openssl-0.0.1.har https://ohpm.openharmony.cn/ohpm/@ohos-rs/openssl/-/openssl-0.0.1.har

# 解压(har实际是一个tar.gz压缩包包)
tar vxf openssl-0.0.1.har

# 进入解压出的文件夹
cd package

# 签名
find . -type f -exec file '{}' ';' | grep --line-buffered ': ELF ' | awk -F': ELF ' '{print $1}' | while read f; do echo "$f"; binary-sign-tool sign -inFile "$f" -outFile "$f" -selfSign 1; done

# 复制到 ~/.local
cp -rv ./prelude/arm64-v8a/* ~/.local

# 测试 cargo
cargo install hello

会得到这样的错误:

localhost ~ % cargo install hello
    Updating crates.io index
error: download of config.json failed

Caused by:
  curl failed

Caused by:
  [60] SSL peer certificate or SSH remote key was not OK (SSL certificate OpenSSL verify result: unable to get local issuer certificate (20))
  1. 安装SSL证书,解决 cargo SSL 错误
# 安装 certifi 证书包
python3 -m pip install --upgrade certifi

# 把 certifi 证书路径加入环境变量
export SSL_CERT_FILE=$(python3 -c 'import certifi; print(certifi.where())')

# 加入 ~/.zshrc 永久生效
echo "export SSL_CERT_FILE=$(python3 -c 'import certifi; print(certifi.where())')" >> ~/.zshrc

# 测试 cargo
cargo install hello

结果:

localhost ~ % cargo install hello
    Updating crates.io index
  Downloaded hello v1.0.4
  Downloaded 1 crate (518B) in 0.96s
error: there is nothing to install in `hello v1.0.4`, because it has no binaries
`cargo install` is only for installing programs, and can't be used with libraries.
To use a library crate, add it as a dependency to a Cargo project with `cargo add`.

现在 cargo 就能正常使用了。

  1. 测试编译rust项目
cargo new hello_world

cd hello_world/src

rustc main.rs

会遇到如下错误:

localhost ~/hello_world/src % rustc main.rs
error: linker `cc` not found
  |
  = note: No such file or directory (os error 2)

error: aborting due to 1 previous error
  1. 解决 linker `cc` not found
# 把clang链接为cc
ln -s $(command -v clang) ~/.local/bin/cc

# 把clang++链接为c++
ln -s $(command -v clang++) ~/.local/bin/c++

# 测试rustc
rustc main.rs

# 尝试执行目标程序
./main

结果:

localhost ~/hello_world/src % rustc main.rs
localhost ~/hello_world/src % ls
main  main.rs
localhost ~/hello_world/src % ./main
Hello, world!
  1. 尝试直接用 cargo 运行项目:
cd ..
cargo run

结果:

localhost ~/hello_world/src % cd ..
localhost ~/hello_world % cargo run
   Compiling hello_world v0.1.0 (/storage/Users/currentUser/hello_world)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.78s
     Running `target/debug/hello_world`
Hello, world!

现在 cargo 和 rustc 就都能用了。


[隐藏样式|查看源码]


『回复列表(0|显示机器人聊天)』

帖子没有回复
回复需要登录

6月12日 21:47 星期五

本站由hu60wap6驱动

备案号: 京ICP备18041936号-1