当前位置:   article > 正文

rust交叉编译-mac编译到linux_could not find directory of openssl installation,

could not find directory of openssl installation, and this `-sys` crate cann

关于交叉编译

一般编程阶段用的是Windows或者Mac系统,部署平台是Linux,这种情况下就需要使用Cross-Compiler交叉编译,意思是可以在当前平台Host下编译出目标平台target的可执行文件,

尤其是做ARM平台开发的同学对这个更为熟悉。

Rust交叉编译在Github上有一个文档Rust核心员工Jorge Aparicio提供的一份文档https://github.com/japaric/rust-cross,推荐大家仔细的读一读。

如果要求比较简单,都是X86_64架构,从Mac上编译出x86_64-unknown-linux-musl就好。

mac上执行

rustup target add x86_64-unknown-linux-musl

 安装musl-cross

brew install filosottile/musl-cross/musl-cross

 

配置和打包编译

配置config

vi ~/.cargo/config(没有新建即可,另外可在项目根目录下创建.cargo/config文件,只对当前项目生效)

添加内容如下:

[target.x86_64-unknown-linux-musl] linker = "x86_64-linux-musl-gcc"

 

cargo new cross-compiling

cd cross-compiling

cargo build --release --target x86_64-unknown-linux-musl

程序简答,确实就是能直接交叉编译了。

但是有的项目用到了openssl,就有可能编译报错

error: failed to run custom build command for openssl-sys v0.9.76

  1. error: failed to run custom build command for `openssl-sys v0.9.76`
  2. Caused by:
  3. process didn't exit successfully: `/Users/xxxxx/target/release/build/openssl-sys-35f194a97bc2188b/build-script-main` (exit status: 101)
  4. --- stdout
  5. cargo:rustc-cfg=const_fn
  6. cargo:rustc-cfg=openssl
  7. cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR
  8. X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR unset
  9. cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  10. OPENSSL_LIB_DIR unset
  11. cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR
  12. X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR unset
  13. cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  14. OPENSSL_INCLUDE_DIR unset
  15. cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR
  16. X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR unset
  17. cargo:rerun-if-env-changed=OPENSSL_DIR
  18. OPENSSL_DIR unset
  19. cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
  20. cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64-unknown-linux-musl
  21. cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64_unknown_linux_musl
  22. cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
  23. cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
  24. cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-musl
  25. cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_musl
  26. cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
  27. cargo:rerun-if-env-changed=PKG_CONFIG
  28. cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
  29. cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
  30. cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
  31. cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
  32. run pkg_config fail: "pkg-config has not been configured to support cross-compilation.\n\nInstall a sysroot for the target platform and configure it via\nPKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a\ncross-compiling wrapper for pkg-config and set it via\nPKG_CONFIG environment variable."
  33. --- stderr
  34. thread 'main' panicked at '
  35. Could not find directory of OpenSSL installation, and this `-sys` crate cannot
  36. proceed without this knowledge. If OpenSSL is installed and this crate had
  37. trouble finding it, you can set the `OPENSSL_DIR` environment variable for the
  38. compilation process.
  39. Make sure you also have the development packages of openssl installed.
  40. For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.
  41. If you're in a situation where you think the directory *should* be found
  42. automatically, please open a bug at https://github.com/sfackler/rust-openssl
  43. and include information about your system as well as this message.
  44. $HOST = x86_64-apple-darwin
  45. $TARGET = x86_64-unknown-linux-musl
  46. openssl-sys = 0.9.76
  47. ', /Users/xxxx/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/openssl-sys-0.9.76/build/find_normal.rs:191:5
  48. note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
  49. warning: build failed, waiting for other jobs to finish...
  50. exit 101

解决

brew install openssl brew install pkg-config brew install perl 我也不知道是不是其中一个就行,我装了,后面也懒得卸载重新验证了

 cargo.toml的[dependencies]添加如下

openssl-sys = "0.9" openssl = { version = "0.10.33", features = ["vendored"] }

 

再次执行cargo build --release --target x86_64-unknown-linux-musl

发现还是报failed to run custom build command for openssl-sys v0.9.76这个错

需要添加一些内容

CROSS_COMPILE=x86_64-linux-musl- cargo build --release --target x86_64-unknown-linux-musl

这样就OK 了

参考

Cross-compiling a simple Rust web app

Rust交叉编译Mac编译Linux/Windows平台 - 走看看

rust - ` error: failed to run custom build command for `openssl-sys v0.9.67` - Stack Overflow

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/189248
推荐阅读
相关标签
  

闽ICP备14008679号