Error migrating from GCC to Clang - undefined reference to atomic
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
Probably need a -latomic. arm5 is awful. I suspect armeabi-v7a works just fine?
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?
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).
Thanks for the help.
@DanAlbert Hey, do you have any feedback on this? Thanks 👍
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.
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).
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.
- cppFlags.add("-latomic")
+ ldFlags.add("-latomic")
It worked, thanks 👍
LOCAL_LDLIBS := -latomic in Android.mk