Skip to content

[BUG] Error when including stdatomic.h inside extern "C" (r21 regression) #1177

Description

@triplef

Description

Including "stdatomic.h" inside an extern "C" block in a C++ file causes the following compile error with NDK r21, while building the same code with r20 works fine:

In file included from test.cpp:2:
In file included from /Users/me/Library/Android/sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/9.0.8/include/stdatomic.h:45:
In file included from /Users/me/Library/Android/sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/c++/v1/atomic:552:
/Users/me/Library/Android/sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/c++/v1/type_traits:426:1: error: templates must have C++ linkage
template <class _T1, class _T2> struct _LIBCPP_TEMPLATE_VIS pair;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:1:1: note: extern "C" language linkage specification begins here
extern "C" {
^

test.cpp:

extern "C" {
#include <stdatomic.h>
}

Compiled using:

$HOME/Library/Android/sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=armv7-none-linux-androideabi21 --gcc-toolchain=$HOME/Library/Android/sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/darwin-x86_64 --sysroot=$HOME/Library/Android/sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -DANDROID -march=armv7-a -mthumb -fPIC -std=gnu++11 -o test.cpp.o -c test.cpp

Using NDK 20.1.5948944 in the above compile command works without issues.

I think the issue was introduced with change 1086558 in bionic:

diff --git a/libc/include/stdatomic.h b/libc/include/stdatomic.h
index 3c61334..b7dac4a 100644
--- a/libc/include/stdatomic.h
+++ b/libc/include/stdatomic.h
@@ -32,7 +32,7 @@
 
 #include <sys/cdefs.h>
 
-#if defined(__cplusplus) && __cplusplus >= 201103L && defined(_USING_LIBCXX)
+#if defined(__cplusplus) && __cplusplus >= 201103L && __has_include(<atomic>)
 # if __has_feature(cxx_atomic)
 #  define _STDATOMIC_HAVE_ATOMIC
 # endif

As far as I can see, _USING_LIBCXX was previously undefined in NDK r20, causing stdatomic.h to use its C implementation instead of including C++ . Note that defined(__cplusplus) will still be true inside the extern "C" block.

Environment Details

  • NDK Version: 21.0.6113669
  • Build system: manually running clang++
  • Host OS: macOS
  • ABI: any
  • NDK API level: any

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions