Rust - 安裝 / 打包#
安裝#
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- cargo 換源
vi $HOME/.cargo/config
內容
[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
Nightly#
# 可通過以下命令安裝 rustc 的 nightly 版本,並將該版本設置為 rustc 的預設版本。
rustup install nightly
rustup default nightly
其他#
# 查看有哪些平台
rustc --print target-list | pr -tw100 --columns 3
# 查看已安裝
rustup target list | grep installed
# 安裝x86_64-unknown-linux-musl
rustup target add x86_64-unknown-linux-musl
更新#
rustup update
打包#
查看依賴#
macOS 查看依賴
otool -L target/release/workout
Linux 查看依賴
ldd target/release/二進制文件
靜態文件#
或者使用cargo install cross --git https://github.com/cross-rs/cross
打包靜態文件可能出現問題,docker 打包最終解決方法!
問題#
blocking waiting for file lock…#
rm -rf ~/.cargo/.package-cache
windows 打包#
q: the msvc targets depend on the msvc linker but link.exe
was not found
解決:
C:\Users\intent\.cargo\bin\rustup uninstall toolchain stable-x86_64-pc-windows-msvc
C:\Users\intent\.cargo\bin\rustup toolchain install stable-x86_64-pc-windows-gnu
C:\Users\intent\.cargo\bin\rustup default stable-x86_64-pc-windows-gnu
或者安裝 VSStudio!!!
q: Internal error occurred: Failed to find tool. Is musl-gcc
installed?#
a: 這個是因為 musl 沒安裝
git clone https://github.com/richfelker/musl-cross-make.git --depth=1
cd musl-cross-make && TARGET=x86_64-linux-musl make -j8
cd build/local/x86_64-linux-musl/ && make install
# 配置好PATH變量
# rust x86_64-linux-musl
export PATH=/mnt/dev/git/rust/musl-cross-make/build/local/x86_64-linux-musl/output/bin:$PATH
q: error: failed to run custom build command for openssl-sys v0.9.72
#
Ubuntu
sudo apt install pkg-config
sudo apt install libudev-dev
sudo apt install librust-openssl-sys-dev
sudo apt install libssl-dev
Gentoo
OPENSSL_LIB_DIR=/usr/lib64 OPENSSL_INCLUDE_DIR=/usr/include TARGET_CC=x86_64-linux-musl-gcc cargo build --release --target=x86_64-unknown-linux-musl
q: reqwest Segmentation fault when building a blocking::Client from a blocking::ClientBuilder#
# Segmentation fault when building a blocking::Client from a blocking::ClientBuilder
OPENSSL_STATIC=true OPENSSL_DIR=/mnt/dev/git/c/openssl-OpenSSL_1_1_1f/musl TARGET_CC=x86_64-linux-musl-gcc cargo build --release --target=x86_64-unknown-linux-musl
q: error: failed to run custom build command for `ring v0.16.20`#
sudo apt install musl-tools
編譯慢,更改 linker 為 llvm#
https://stackoverflow.com/questions/57812916/how-do-i-change-the-default-rustc-cargo-linker