bbqsrc / cargo-ndk

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

x86_64 linker issue #122

Closed NiallBunting closed 1 year ago

NiallBunting commented 1 year ago

I'm running the command: cargo ndk -o ./android -t armeabi-v7a -t x86_64 build The armeabi builds correctly but I run into a linker issue with the x86_64 build.

Completing with = note: ld.lld: error: unable to find library -lclang_rt.builtins-x86_64-android clang-17: error: linker command failed with exit code 1 (use -v to see invocation) The full output is here: https://pastebin.com/sgbyYEUh

I checked NDK to see if that builtin exists and it seems to be here: /home/nib/Android/Sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/17/lib/linux/libclang_rt.builtins-x86_64-android.a.

Also ensured that rustup was correct: nib@nib-desktop:~/repos/matrix-rust-sdk/bindings/matrix-sdk-ffi$ rustup target install x86_64-linux-android info: component 'rust-std' for target 'x86_64-linux-android' is up to date.

igor-brishkoski commented 1 year ago

Any updates here? Facing the same issue.

java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "__extenddftf2" referenced by "/data/app/~~9i8SALkmFVE3inoqZObfVQ==/com.example.android-LszkPsK4KaEepki_EkBFdA==/base.apk!/lib/x86_64/libtest.so
bbqsrc commented 1 year ago

I checked NDK to see if that builtin exists and it seems to be here: /home/nib/Android/Sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/17/lib/linux/libclang_rt.builtins-x86_64-android.a.

Builds correctly for me on macOS with NDK v26.0.10792818, so I suspect the NDK is doing nonsensical things again, or you need to update your cargo-ndk.

bbqsrc commented 1 year ago

@NiallBunting this is an issue with matrix-rust-sdk. You need to set NDK_CLANG_VERSION to the clang version relative to the NDK you're targetting. See their build.rs file.

igor-brishkoski commented 1 year ago

Not sure why you closed this, seems other people are having the same issue with different libraries.

https://github.com/tauri-apps/tauri/issues/6047

bbqsrc commented 1 year ago

This has nothing to do with cargo-ndk and everything to do with not linking to the correct libraries.

NiallBunting commented 1 year ago

Ah I think it might actually be caused by this: https://github.com/rust-lang/rust/issues/109717

9p4 commented 6 months ago

Made some changes to matrix-rust-sdk to get this working:

diff --git a/bindings/matrix-sdk-crypto-ffi/build.rs b/bindings/matrix-sdk-crypto-ffi/build.rs
index a3572f17f..39663cab2 100644
--- a/bindings/matrix-sdk-crypto-ffi/build.rs
+++ b/bindings/matrix-sdk-crypto-ffi/build.rs
@@ -18,13 +18,16 @@ fn setup_x86_64_android_workaround() {
                 "Unsupported OS. You must use either Linux, MacOS or Windows to build the crate."
             ),
         };
-        const DEFAULT_CLANG_VERSION: &str = "14.0.7";
+        const DEFAULT_CLANG_VERSION: &str = "17";
+        const DEFAULT_NDK_VERSION: &str = "26.3.11579264";
         let clang_version =
             env::var("NDK_CLANG_VERSION").unwrap_or_else(|_| DEFAULT_CLANG_VERSION.to_owned());
+        let ndk_version =
+            env::var("NDK_VERSION").unwrap_or_else(|_| DEFAULT_NDK_VERSION.to_owned());
         let linux_x86_64_lib_dir = format!(
-            "toolchains/llvm/prebuilt/{build_os}-x86_64/lib64/clang/{clang_version}/lib/linux/"
+            "toolchains/llvm/prebuilt/{build_os}-x86_64/lib/clang/{clang_version}/lib/linux/"
         );
-        println!("cargo:rustc-link-search={android_ndk_home}/{linux_x86_64_lib_dir}");
+        println!("cargo:rustc-link-search={android_ndk_home}/{ndk_version}/{linux_x86_64_lib_dir}");
         println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android");
     }
 }
diff --git a/bindings/matrix-sdk-ffi/build.rs b/bindings/matrix-sdk-ffi/build.rs
index e22f54451..217b28750 100644
--- a/bindings/matrix-sdk-ffi/build.rs
+++ b/bindings/matrix-sdk-ffi/build.rs
@@ -18,13 +18,16 @@ fn setup_x86_64_android_workaround() {
                 "Unsupported OS. You must use either Linux, MacOS or Windows to build the crate."
             ),
         };
-        const DEFAULT_CLANG_VERSION: &str = "14.0.7";
+        const DEFAULT_CLANG_VERSION: &str = "17";
+        const DEFAULT_NDK_VERSION: &str = "26.3.11579264";
         let clang_version =
             env::var("NDK_CLANG_VERSION").unwrap_or_else(|_| DEFAULT_CLANG_VERSION.to_owned());
+        let ndk_version =
+            env::var("NDK_VERSION").unwrap_or_else(|_| DEFAULT_NDK_VERSION.to_owned());
         let linux_x86_64_lib_dir = format!(
-            "toolchains/llvm/prebuilt/{build_os}-x86_64/lib64/clang/{clang_version}/lib/linux/"
+            "toolchains/llvm/prebuilt/{build_os}-x86_64/lib/clang/{clang_version}/lib/linux/"
         );
-        println!("cargo:rustc-link-search={android_ndk_home}/{linux_x86_64_lib_dir}");
+        println!("cargo:rustc-link-search={android_ndk_home}/{ndk_version}/{linux_x86_64_lib_dir}");
         println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android");
     }
 }