ndk icon indicating copy to clipboard operation
ndk copied to clipboard

Error migrating from GCC to Clang - undefined reference to atomic

Open 4brunu opened this issue 10 years ago • 11 comments

Hi,

I have a project that uses C++ and I'm using Djinni to create the JNI code. This works fine with GCC, but after some attempts to migrate to Clang, I always get this error.

/Applications/android-sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/ext/atomicity.h
Error:(53) undefined reference to '__atomic_fetch_add_4'
Error:(49) undefined reference to '__atomic_fetch_add_4'
Error:(49) undefined reference to '__atomic_fetch_add_4'
Error:(53) undefined reference to '__atomic_fetch_add_4'
/Applications/android-sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/bits/shared_ptr_base.h
Error:(199) undefined reference to '__atomic_load_4'
Error:error: linker command failed with exit code 1 (use -v to see invocation)
Error:Execution failed for task ':app:linkExampleArmeabiDebugSharedLibrary'.
> A build operation failed.
      Linker failed while linking libexample.so.

I created a sample project to show my issue. https://github.com/4brunu/AndroidNDK_GCC_To_Clang

I have tested with NDK r11c and r12-beta2. Am I doing something wrong, or it's a NDK bug?

Thanks

4brunu avatar May 23 '16 21:05 4brunu

Probably need a -latomic. arm5 is awful. I suspect armeabi-v7a works just fine?

DanAlbert avatar May 23 '16 21:05 DanAlbert

Thanks for the quick response.

I had tried to add -latomic but the error persists.

Here is my NDK configuration.

        ndk {
//            toolchain = "gcc"
//            toolchainVersion = "4.9"
            toolchain = "clang"

            moduleName = "example"

            stl = "gnustl_static"

            cppFlags.add("-std=c++11")
            cppFlags.add("-fexceptions")
            cppFlags.add("-frtti")
            cppFlags.add("-latomic")

            // djinni generated-src
            cppFlags.add("-I${file("../../generated-src/cpp")}".toString())
            //djinni
            cppFlags.add("-I${file("../../deps/djinni/support-lib/jni")}".toString())
        }

Sorry, but I think I didn't get the last part. I know those are CPU architecture, but do you want me to do something? Like trying to build against a armeabi-v7a cpu?

4brunu avatar May 23 '16 21:05 4brunu

I'm not familiar with gradle, so idk how to specify it in your build, but in the ndk-build world it would be APP_ABI := armeabi-v7a.

Either way we should find a solution for armeabi, but it might help narrow down the issue.

I'll try to find some time to look at your repro case (thanks for providing that, btw, should make it easier).

DanAlbert avatar May 23 '16 21:05 DanAlbert

Thanks for the help.

4brunu avatar May 23 '16 22:05 4brunu

@DanAlbert Hey, do you have any feedback on this? Thanks 👍

4brunu avatar Jun 09 '16 12:06 4brunu

Haven't had a chance to look yet. Been busy with the r12 release and now a libc++ update. Once that's done I'll be taking a pass through all our open bugs though.

DanAlbert avatar Jun 09 '16 21:06 DanAlbert

How do I actually build your sample? I don't see any obvious build tasks when running gradlew (we only use ndk-build ourselves, so this could be something obvious I'm missing).

DanAlbert avatar Jul 12 '16 21:07 DanAlbert

Just open the Android Studio project, hit run, and you will get some errors.

Check this lines to change between clang and gcc. https://github.com/4brunu/AndroidNDK_GCC_To_Clang/blob/master/AndroidNDK_GCC_To_Clang/app/build.gradle#L53-L55

With gcc work's, but not with clang.

4brunu avatar Jul 12 '16 22:07 4brunu

-            cppFlags.add("-latomic")
+            ldFlags.add("-latomic")

DanAlbert avatar Jul 13 '16 22:07 DanAlbert

It worked, thanks 👍

4brunu avatar Jul 14 '16 10:07 4brunu

LOCAL_LDLIBS := -latomic in Android.mk

ElephantInTheRoomFirst avatar Jun 25 '17 07:06 ElephantInTheRoomFirst