mac m1上使用rust编译openssl-sys失败的一次记录

mac m1 编译openssl-sys的时候报错:

遇到错误,报错信息如下

admin@admindeMacBook-Air b3 % cargo run
   Compiling openssl-sys v0.9.72
error: failed to run custom build command for `openssl-sys v0.9.72`

Caused by:
  process didn't exit successfully: `/Users/admin/project/b3/target/debug/build/openssl-sys-7ab7e6047ef6ec59/build-script-main` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR
  AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR
  AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_DIR
  AARCH64_APPLE_DARWIN_OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_DIR
  OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_aarch64-apple-darwin
  cargo:rerun-if-env-changed=PKG_CONFIG_aarch64_apple_darwin
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=OPENSSL_STATIC
  cargo:rerun-if-env-changed=OPENSSL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_aarch64-apple-darwin
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_aarch64_apple_darwin
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_aarch64-apple-darwin
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_aarch64_apple_darwin
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64-apple-darwin
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64_apple_darwin
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
  run pkg_config fail: "Could not run `\"pkg-config\" \"--libs\" \"--cflags\" \"openssl\"`\nThe pkg-config command could not be found.\n\nMost likely, you need to install a pkg-config package for your OS.\nTry `brew install pkg-config` if you have Homebrew.\n\nIf you've already installed it, ensure the pkg-config command is one of the\ndirectories in the PATH environment variable.\n\nIf you did not expect this build to link to a pre-installed system library,\nthen check documentation of the openssl-sys crate for an option to\nbuild the library from source, or disable features or dependencies\nthat require pkg-config."

  --- stderr
  thread 'main' panicked at '

  Could not find directory of OpenSSL installation, and this `-sys` crate cannot
  proceed without this knowledge. If OpenSSL is installed and this crate had
  trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
  compilation process.

  Make sure you also have the development packages of openssl installed.
  For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.

  If you're in a situation where you think the directory *should* be found
  automatically, please open a bug at https://github.com/sfackler/rust-openssl
  and include information about your system as well as this message.

  $HOST = aarch64-apple-darwin
  $TARGET = aarch64-apple-darwin
  openssl-sys = 0.9.72

  ', /Users/admin/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/openssl-sys-0.9.72/build/find_normal.rs:180:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

同样的代码在ubuntu上正常,但是在mac m1上面无法编译,根据报错信息所以猜测是openssl-sys这个库需要调用本机上的openssl,但是无法找到。
找到了一篇文档,
https://reality0ne.com/macos-xia-shi-yong-rust-openssl/

执行文章中的命令:

$ cargo clean
$ export OPENSSL_LIB_DIR=$(brew --prefix openssl)/lib
$ export OPENSSL_INCLUDE_DIR=$(brew --prefix openssl)/include
$ cargo build
$ cargo test

然后到项目里面cargo run一下,仍然报错

admin@admindeMacBook-Air b3 % cargo run
   Compiling openssl-sys v0.9.72
   Compiling socket2 v0.4.3
   Compiling memmap2 v0.5.3
   Compiling rand v0.4.6
   Compiling memmap v0.7.0
   Compiling quote v1.0.15
   Compiling twoway v0.1.8
   Compiling buf_redux v0.8.4
error: failed to run custom build command for `openssl-sys v0.9.72`

Caused by:
  process didn't exit successfully: `/Users/admin/project/b3/target/debug/build/openssl-sys-7ab7e6047ef6ec59/build-script-main` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR
  AARCH64_APPLE_DARWIN_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR = /opt/homebrew/opt/openssl@3/lib
  cargo:rerun-if-env-changed=AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR
  AARCH64_APPLE_DARWIN_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR = /opt/homebrew/opt/openssl@3/include

  --- stderr
  thread 'main' panicked at 'OpenSSL library directory does not exist: /opt/homebrew/opt/openssl@3/lib', /Users/admin/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/openssl-sys-0.9.72/build/main.rs:68:9
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...

这个提示大概看懂了,意思是openssl的库在/opt/homebrew/opt/openssl@3/lib下不存在,cd到这个目录之后发现确实不存在

admin@admindeMacBook-Air opt % ls
pkg-config      pkgconfig

所以打算用homebrew重新安装一下,这里使用了这个作者的方法
https://zhuanlan.zhihu.com/p/111014448 (homebrew国内镜像安装)
然后执行brew install openssl

执行过程:

admin@admindeMacBook-Air opt % brew install openssl
Error: 
  homebrew-core is a shallow clone.
  homebrew-cask is a shallow clone.
To `brew update`, first run:
  git -C /opt/homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
  git -C /opt/homebrew/Library/Taps/homebrew/homebrew-cask fetch --unshallow
These commands may take a few minutes to run due to the large size of the repositories.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/ca-certificates-2022-03-29.all.bottle.tar.gz
######################################################################## 100.0%
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/openssl%403-3.0.2.arm64_big_sur.bottle.tar.gz
######################################################################## 100.0%
==> Installing dependencies for openssl@3: ca-certificates
==> Installing openssl@3 dependency: ca-certificates
==> Pouring ca-certificates-2022-03-29.all.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
🍺  /opt/homebrew/Cellar/ca-certificates/2022-03-29: 3 files, 211.5KB
==> Installing openssl@3
==> Pouring openssl@3-3.0.2.arm64_big_sur.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /opt/homebrew/etc/openssl@3/certs

and run
  /opt/homebrew/opt/openssl@3/bin/c_rehash

openssl@3 is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS provides LibreSSL.

If you need to have openssl@3 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@3 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"

For pkg-config to find openssl@3 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@3/lib/pkgconfig"

==> Summary
🍺  /opt/homebrew/Cellar/openssl@3/3.0.2: 6,429 files, 28.0MB
==> Running `brew cleanup openssl@3`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> openssl@3
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /opt/homebrew/etc/openssl@3/certs

and run
  /opt/homebrew/opt/openssl@3/bin/c_rehash

openssl@3 is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS provides LibreSSL.

If you need to have openssl@3 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@3 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"

For pkg-config to find openssl@3 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@3/lib/pkgconfig"

安装完成之后然后再ls一下,发现目录下 openssl@3 在目录下面了

admin@admindeMacBook-Air opt % ls                  
ca-certificates openssl         openssl@3       pkg-config      pkgconfig

这样的话,环境变量OPENSSL_LIB_DIR、OPENSSL_INCLUDE_DIR指向的目录也都存在了,
然后再cargo run 一下,发现程序正常了,问题解决!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Rust 编程语言来实现一个 STUN 客户端。STUN(Simple Traversal of UDP through NATs)是一种用于网络地址转换(NAT)穿透的协议。 下面是一个简单的示例代码,演示了如何使用 Rust 和 tokio 库来实现一个基本的 STUN 客户端: ```rust use std::net::{SocketAddr, UdpSocket}; use tokio::runtime::Runtime; fn main() { // 设置 STUN 服务器的地址 let stun_server: SocketAddr = "stun.example.com:3478".parse().unwrap(); // 创建 UDP 套接字 let socket = UdpSocket::bind("0.0.0.0:0").expect("Failed to bind socket"); // 发送 STUN 请求 let mut buf = [0u8; 1024]; let request: [u8; 20] = [ 0x00, 0x01, 0x00, 0x00, 0x21, 0x12, 0xA4, 0x42, // STUN 请求头部 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // STUN 请求数据 0x00, 0x08, 0x00, 0x14, // STUN 请求数据长度 ]; socket .send_to(&request, stun_server) .expect("Failed to send STUN request"); // 接收 STUN 响应 let (recv_len, _) = socket.recv_from(&mut buf).expect("Failed to receive STUN response"); let response = &buf[..recv_len]; // 处理 STUN 响应 // 这里可以根据 STUN 协议解析 response 数据 println!("STUN response: {:?}", response); } ``` 这只是一个简单的示例,实际上你可能需要更多的代码来处理 STUN 协议的各种情况和错误处理。你可以使用其他 Rust 库来帮助解析和处理 STUN 响应数据。 请注意,这个示例使用了 tokio 库来实现异步网络编程。你需要在 `Cargo.toml` 文件中添加相应的依赖,例如: ```toml [dependencies] tokio = { version = "1", features = ["full"] } ``` 希望这个示例能对你有所帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值