bbqsrc / cargo-ndk

Compile Rust projects against the Android NDK without hassle
Apache License 2.0
712 stars 64 forks source link

C linker issue #114

Closed randrievskiy closed 1 year ago

randrievskiy commented 1 year ago

We've encountered an Issue:

ld: error: unable to find library -lgcc  
clang-14: error: linker command failed with exit code 1 

We need to link Rust code with Harfbuzz and run it on Android. Bindings are in the separate Cargo module.

We've already googled a lot and tried a bunch of workarounds(e.g., add libgcc.a files, build with NDKs lower than 25). Unfortunately, nothing helps( Hope you can help us solve this)

cargo-ndk - 3.2.0 Android NDK - 25.2.9519653 Rust version - 1.73.0-nightly (da6b55cc5 2023-07-17)

Building with command:

cargo ndk -o ../app/src/main/jniLibs  build --release -Zbuild-std=panic_abort,std

Cargo.toml

[package]
name = "text"
version = "0.1.0"
edition = "2021"

[profile.release]
lto = true

[lib]
name = "_text"
crate-type = ["cdylib", "rlib", "staticlib"]

[dependencies]
tokio = "1.29.1"
log = "0.4.19"
android_logger = "0.13.1"
jni = "0.21.1"
async-std = "1.12.0"
futures = "0.3.28"
openssl = { version = "0.10", features = ["vendored"] }
openssl-sys = "0.9.90"

cppdeps = { path = "../text/cppdeps" }
rib commented 1 year ago

libgcc is no longer in the Android ndk, since r23.

How are you building harfbuzz and linking with it I wonder? I'm wondering if you're using any pre-built binaries that maybe need to be recompiled with a newer ndk toolchain so they don't try to pull in libgcc.

It seems like you need to track down which crate / library is trying to link against libgcc.

It used to be that the Rust stdlib binaries distributed with the rust compiler linked against libgcc and so tools like cargo-ndk used to implement workaround for redirecting the linker to link with libunwind instead of libgcc but that shouldn't generally be necessary any more.

As a workaround though maybe you could experiment with installing cargo-ndk 2 which used to implement the libgcc -> libunwind workaround: https://github.com/bbqsrc/cargo-ndk/pull/67

randrievskiy commented 1 year ago

We're using Bindgen

bindgen::Builder::default()
    .header("packages/harfbuzz_deps.h") // #include https://github.com/harfbuzz/harfbuzz/blob/main/src/hb.h
    .clang_arg("-fvisibility=default")
    .parse_callbacks(Box::new(bindgen::CargoCallbacks))
    .generate()
    .expect("Unable to generate bindings");
rib commented 1 year ago

but how are you compiling the harfbuzz library itself which is written in C++ from what I recall. I guess you maybe either have a pre-built binary for harfbuzz or maybe a build.rs that cross compiles via autotools.

If you're building a .so for harfbuzz then you could try using objdump or readelf to check the list of dependencies that the library has and double check that doesn't include libgcc.so

randrievskiy commented 1 year ago

Yes, we have a build.rs and we're compiling with Meson

rib commented 1 year ago

okey yeah.

can you maybe try running

readelf -d path/to/libharfbuzz.so | grep 'NEEDED'

and see if it needs libgcc.so

If so then I guess you may need to look at how the meson build is done.

randrievskiy commented 1 year ago

Okay, I've found a solution to my issue. I forgot to add a cross-file for the Meson. Thank you for your time!