SSL linking error while cross compiling for android

I want to cross compile my project for Android. I've created ~/.cargo/config as follows:-

[target.armv7-linux-androideabi]
ar = "/optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar"
linker = "/optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi26-clang"

Here is the Cargo.toml file for my project:-

[package]
name = "examer"
version = "0.1.0"
authors = ["none"]
edition = "2018"

[dependencies]
tokio = { version = "0.2", features = ["full"] }
lazy_static = "1"
clap = "2.*"
futures = { version = "0.3", default-features = false, features = ["alloc"] }
futures-util = "0.3"
async-tungstenite = { version = "*", features= ["tokio-runtime"] }
mongodb =  { version = "1.*", features = ["tokio-runtime"] }
pbkdf2 = "0.2"
# serde = { version = "1.0", features = ["derive", "rc"] }
serde_cbor = "0.11"
derive_builder = "0.9.0"
# serde_derive = "1.0"

[dev-dependencies]
tungstenite = "0.11"
url = "2"

[lib]
name = "libexamer"
path = "src/lib.rs"

[[bin]]
name = "examer"
path = "src/main.rs"                                                                                                                            

Its compiling and working fine on Desktop linux

But its not compiling for Android Linux. I have added target armv7-linux-androideabi with rustup

❯ cargo build --target armv7-linux-androideabi

      ... Compling Very long list of packages So i trimmed it...

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

Caused by:
  process didn't exit successfully: `/home/noone/examer/target/debug/build/openssl-sys-d34551fe3fadea5f/build-script-main` (exit code: 101)
--- stdout
cargo:rustc-cfg=const_fn
cargo:rerun-if-env-changed=ARMV7_LINUX_ANDROIDEABI_OPENSSL_LIB_DIR
ARMV7_LINUX_ANDROIDEABI_OPENSSL_LIB_DIR unset
cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
OPENSSL_LIB_DIR unset
cargo:rerun-if-env-changed=ARMV7_LINUX_ANDROIDEABI_OPENSSL_INCLUDE_DIR
ARMV7_LINUX_ANDROIDEABI_OPENSSL_INCLUDE_DIR unset
cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
OPENSSL_INCLUDE_DIR unset
cargo:rerun-if-env-changed=ARMV7_LINUX_ANDROIDEABI_OPENSSL_DIR
ARMV7_LINUX_ANDROIDEABI_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_ALLOW_CROSS_armv7-linux-androideabi
cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_armv7_linux_androideabi
cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
cargo:rerun-if-env-changed=PKG_CONFIG_armv7-linux-androideabi
cargo:rerun-if-env-changed=PKG_CONFIG_armv7_linux_androideabi
cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_armv7-linux-androideabi
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_armv7_linux_androideabi
cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
run pkg_config fail: "Cross compilation detected. Use PKG_CONFIG_ALLOW_CROSS=1 to override"

--- 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 = x86_64-unknown-linux-gnu
$TARGET = armv7-linux-androideabi
openssl-sys = 0.9.58

', /home/noone/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.58/build/find_normal.rs:157:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

warning: build failed, waiting for other jobs to finish...
error: build failed

So i next tried to add PKG_CONFIG_ALLOW_CROSS=1 but it also fails

❯ PKG_CONFIG_ALLOW_CROSS=1 cargo build --target armv7-linux-androideabi
   Compiling ring v0.16.15
   Compiling openssl-sys v0.9.58
   Compiling proc-macro2 v0.4.30
   Compiling httparse v1.3.4
   Compiling quote v1.0.7
   Compiling getrandom v0.1.14
   Compiling generic-array v0.12.3
   Compiling net2 v0.2.34
error: failed to run custom build command for `ring v0.16.15`

Caused by:
  process didn't exit successfully: `/home/noone/examer/target/debug/build/ring-039ac27f87884aa1/build-script-build` (exit code: 101)
--- stdout
OPT_LEVEL = Some("0")
TARGET = Some("armv7-linux-androideabi")
HOST = Some("x86_64-unknown-linux-gnu")
CC_armv7-linux-androideabi = None
CC_armv7_linux_androideabi = None
TARGET_CC = None
CC = None
CFLAGS_armv7-linux-androideabi = None
CFLAGS_armv7_linux_androideabi = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")

--- stderr
ENV ANDROID_HOME=/optHDD/AndroidSDK
ENV ANDROID_NDK=/optHDD/AndroidNDK
ENV ANDROID_NDK_ROOT=/optHDD/AndroidNDK
ENV ANDROID_SDK_ROOT=/optHDD/AndroidSDK
ENV BINARYEN=/usr
ENV BINARYEN_ROOT=/usr
ENV CARGO=/home/noone/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo
ENV CARGO_CFG_TARGET_ARCH=arm
ENV CARGO_CFG_TARGET_ENDIAN=little
ENV CARGO_CFG_TARGET_ENV=
ENV CARGO_CFG_TARGET_FAMILY=unix
ENV CARGO_CFG_TARGET_OS=android
ENV CARGO_CFG_TARGET_POINTER_WIDTH=32
ENV CARGO_CFG_TARGET_VENDOR=unknown
ENV CARGO_CFG_UNIX=
ENV CARGO_FEATURE_ALLOC=1
ENV CARGO_FEATURE_DEFAULT=1
ENV CARGO_FEATURE_DEV_URANDOM_FALLBACK=1
ENV CARGO_FEATURE_ONCE_CELL=1
ENV CARGO_HOME=/home/noone/.cargo
ENV CARGO_MAKEFLAGS=--jobserver-fds=5,6 -j --jobserver-auth=5,6 -j
ENV CARGO_MANIFEST_DIR=/home/noone/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.15
ENV CARGO_MANIFEST_LINKS=ring-asm
ENV CARGO_PKG_AUTHORS=Brian nooneh <brian@briannooneh.org>
ENV CARGO_PKG_DESCRIPTION=Safe, fast, small crypto using Rust.
ENV CARGO_PKG_HOMEPAGE=
ENV CARGO_PKG_NAME=ring
ENV CARGO_PKG_REPOSITORY=https://github.com/briannooneh/ring
ENV CARGO_PKG_VERSION=0.16.15
ENV CARGO_PKG_VERSION_MAJOR=0
ENV CARGO_PKG_VERSION_MINOR=16
ENV CARGO_PKG_VERSION_PATCH=15
ENV CARGO_PKG_VERSION_PRE=
ENV LD_LIBRARY_PATH=/home/noone/examer/target/debug/deps:/home/noone/examer/target/debug:/home/noone/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib:/home/noone/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib
ENV NUM_JOBS=8
ENV OPT_LEVEL=0
ENV OUT_DIR=/home/noone/examer/target/armv7-linux-androideabi/debug/build/ring-bb7f58c397198b7c/out
ENV PATH=/home/noone/.cargo/bin:/opt/google-cloud-sdk/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/lib/emscripten:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/noone/.gem/ruby/2.7.0/bin:/opt/scripts:/usr/lib/servo:/home/noone/.rvm/bin
ENV PKG_CONFIG_ALLOW_CROSS=1
ENV PROFILE=debug
ENV RUSTC=rustc
ENV RUSTC_LINKER=/optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi26-clang
ENV RUSTDOC=rustdoc
ENV RUSTUP_HOME=/home/noone/.rustup
ENV RUSTUP_TOOLCHAIN=stable-x86_64-unknown-linux-gnu
ENV RUST_RECURSION_COUNT=1
ENV SHELL=/bin/sh
ENV SHELL_SESSION_ID=2f2df9d05d444290bf71a1767feb62d7
ENV SHLVL=1
ENV SSL_CERT_DIR=/etc/ssl/certs
ENV SSL_CERT_FILE=/etc/ssl/cert.pem
ENV TARGET=armv7-linux-androideabi
ENV _=/usr/bin/cargo
running "arm-linux-androideabi-clang" "-O0" "-DANDROID" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=armv7-linux-androideab-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wenum-compare" "-Wfloat-equal" "-Wformat"-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compare" "-Wsign-conversion" "-Wundef" "-Wunialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-g3" "-DNDEBUG" "-c" "-o/home/noone/examer/target/armv7-linux-androideabi/debuild/ring-bb7f58c397198b7c/out/aesv8-armx-linux32.o" "/home/noone/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.15/pregenerated/aesv8-armx-linux32.S"
thread 'main' panicked at 'failed to execute ["arm-linux-androideabi-clang" "-O0" "-DANDROID" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-poi" "--target=armv7-linux-androideabi" "-I" "include" "-Wall" "-Wextra" "-pedantic" "-pedantic-errors" "-Wall" "-Wextra" "-Wcast-align" "-Wcast-qual" "-Wconversion" "-Wecompare" "-Wfloat-equal" "-Wformat=2" "-Winline" "-Winvalid-pch" "-Wmissing-field-initializers" "-Wmissing-include-dirs" "-Wredundant-decls" "-Wshadow" "-Wsign-compareWsign-conversion" "-Wundef" "-Wuninitialized" "-Wwrite-strings" "-fno-strict-aliasing" "-fvisibility=hidden" "-fstack-protector" "-g3" "-DNDEBUG" "-c" "-o/home/noone/ex/target/armv7-linux-androideabi/debug/build/ring-bb7f58c397198b7c/out/aesv8-armx-linux32.o" "/home/noone/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.15/prerated/aesv8-armx-linux32.S"]: No such file or directory (os error 2)', /home/noone/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.15/build.rs:661:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

warning: build failed, waiting for other jobs to finish...
error: build failed

After some googling, I found that people suggest to manually set AR and CC environment variabls So i did that too

❯ CC=/optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi26-clang AR=/optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar PKG_CONFIG_ALLOW_CROSS=1  cargo build --target armv7-linux-androideabi
   Compiling openssl-sys v0.9.58
      ... Compling Very long list of packages So i trimmed it...
   
error: linking with `/optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi26-clang` failed: exit code: 1
  |
  = note: "/optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi26-clang" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--allow-multiple-definition" "-march=armv7-a" "-L" <...very long list of files...> "-Wl,-Bdynamic" "-lssl" "-lcrypto" "-ldl" "-llog" "-lgcc" "-lc" "-lm"
  = note: /optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lssl
          /optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lcrypto
          /home/noone/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.10.30/src/error.rs:184: error: undefined reference to 'ERR_lib_error_string'
          /home/noone/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.10.30/src/error.rs:196: error: undefined reference to 'ERR_func_error_string'
          /home/noone/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.10.30/src/error.rs:208: error: undefined reference to 'ERR_reason_error_string'
          /home/noone/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.10.30/src/x509/mod.rs:1272: error: undefined reference to 'X509_verify_cert_error_string'
          /home/noone/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.58/src/lib.rs:105: error: undefined reference to 'OPENSSL_init_ssl'
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: aborting due to previous error

error: could not compile `examer`.

To learn more, run the command again with --verbose.

Your android SDK will not have the ssl or crypto libraries that are part of OpenSSL, as these are not preinstalled:

  = note: /optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lssl
          /optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lcrypto

This is a general problem when cross compiling something that uses C libraries. I think you'll have to cross-compile OpenSSL separately from source, as a static library for Android.

1 Like

Never ever set this if you're not also configuring an alternative sysroot for pkg-config. If you just set this, you're making things worse, because pkg-config will find Linux-x86 libraries and add them to your Android-ARM build. This can't possibly work, and will make more, worse errors.

2 Likes

I have found the solution

I have added following to my project's Cargo.toml

[target.armv7-linux-androideabi.dependencies]
openssl = { version = "*", features = ["vendored"] }

I think it downloads openssl source and statically link it to my app

also I think ~/.cargo/config file is useless as I have to set CC and AR environment variables manually before compilation like this

export CC=/optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi26-clang
export AR=/optHDD/AndroidNDK/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar
cargo build --target armv7-linux-androideabi
2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.