__atomic_fetch_add undefined with Clang (r9b)

1,329 views
Skip to first unread message

Brent Lewis

unread,
Nov 22, 2013, 8:11:06 PM11/22/13
to andro...@googlegroups.com, Jake Brownson
I see in the revisions list for 9b that it:
  • Fixed Clang 3.3 to compile ARM's built-in, atomic operations such as __atomic_fetch_add,__atomic_fetch_sub, and __atomic_fetch_or.
It does properly generate the needed variety of __atomic_fetch_add_n, but:

C:/android-ndk-r9b/sources/cxx-stl/gnu-libstdc++/4.8/include\bits/atomic_base.h:614: error: undefined reference to '__atomic_fetch_add_4'

Repro: In Eclipse, make an application project called 'Test'. Add native support. Create an Application.mk:

NDK_TOOLCHAIN_VERSION := clang
APP_STL:=gnustl_shared

In Android.mk, add:

LOCAL_CPPFLAGS := -std=c++11

Replace the contents of Test.cpp with:

#include <atomic>

void test() {
std::atomic<int> a(0);
a++;
}

Build the project to observe the error. Is this a known issue? Have I done something incorrectly? I have not been able to find any mention of this particular problem.

I tried adding this to the end of Test.cpp:

typedef int I4;

I4 __atomic_fetch_add_4 (I4 *mem, I4 val, int model) {
return __sync_fetch_and_add(mem, val);
}

based on the signature here, but I suspect something about it is wrong because clang still says its undefined. (also tried adding volatile to the first parameter and/or changing the last parameter to std::memory_model)

Any info on this would be helpful. Thanks.

Complete build log:

**** Build of configuration Default for project Test ****

C:\android-ndk-r9b\ndk-build.cmd all 
Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 8 in ./AndroidManifest.xml    
[armeabi] Compile++ thumb: Test <= Test.cpp
[armeabi] Prebuilt       : libgnustl_shared.so <= <NDK>/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi/
[armeabi] SharedLibrary  : libTest.so
C:/android-ndk-r9b/sources/cxx-stl/gnu-libstdc++/4.8/include\bits/atomic_base.h:614: error: undefined reference to '__atomic_fetch_add_4'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
make.exe: *** [obj/local/armeabi/libTest.so] Error 1

**** Build Finished ****

Nathan Daly

unread,
Jul 12, 2015, 7:46:24 AM7/12/15
to andro...@googlegroups.com, ja...@brainiumstudios.com
I know it's been a while since this post, but I am seeing the same issue when I try to build with:

# Application.mk
APP_ABI := armeabi armeabi-v7a x86
APP_STL := c++_static
APP_CPPFLAGS += -std=c++11
APP_CPPFLAGS += -fexceptions

[armeabi] SharedLibrary  : libmain.so
/Users/Daly/Documents/School/EECS/Downloads/Frameworks/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux
-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: warning: hidden symbol '__cxa_call_unexpected' in /Users/Daly/Documents/School/EECS/Downloads/Fra
meworks/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libs/armeabi/thumb/libc++_static.a(cxa_personality.o) is referenced by DSO ./obj/local/armeabi/libSDL2.
so
/tmp/ndk-user/tmp/build-libc++/ndk/sources/cxx-stl/llvm-libc++/libcxx/include/atomic:922: error: undefined reference to '__atomic_fetch_add_4'

Nathan Daly

unread,
Jul 12, 2015, 4:13:02 PM7/12/15
to andro...@googlegroups.com, ja...@brainiumstudios.com
I ended up fixing this by switching to the c++_shared library instead. It's more of a work-around than a fix, but it worked(-around) successfully for me.

--
You received this message because you are subscribed to a topic in the Google Groups "android-ndk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/android-ndk/LaAILr3FSho/unsubscribe.
To unsubscribe from this group and all its topics, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages