ndk icon indicating copy to clipboard operation
ndk copied to clipboard

Unified Headers lack C++11 support

Open ddgcodes opened this issue 8 years ago • 4 comments

Description

With the release of NDKr16, I have switched over to using Clang and libc++ for my project. However, in doing so, I discovered that while the Clang headers are complete and correct, the Unified Headers are not, at least with respect to C++11 support. This results in an inability to use a number of methods as they are incorrectly defined. See the following snippet from libc++'s <cmath> header:

using ::isgreater; using ::isgreaterequal; using ::isless; using ::islessequal; using ::islessgreater; using ::isunordered; using ::isunordered;

These are intended to import the equivalent methods from the global namespace into std, in compliance with the C++11 spec. However, going into the Unified Headers <math.h>, where these methods are supposed to be defined, we instead have the old C99 macros:

#define isgreater(x, y) __builtin_isgreater((x), (y)) #define isgreaterequal(x, y) __builtin_isgreaterequal((x), (y)) #define isless(x, y) __builtin_isless((x), (y)) #define islessequal(x, y) __builtin_islessequal((x), (y)) #define islessgreater(x, y) __builtin_islessgreater((x), (y)) #define isunordered(x, y) __builtin_isunordered((x), (y))

To reproduce, simply create a .cpp file, #include <cmath> and compile using Clang with libc++ and the -std=c++11 flag. You don't even need to attempt to call any function; <cmath> itself fails to compile.

Environment Details

  • NDK Version: NDKr16b
  • Build sytem: Standalone toolchain (Visual Studio)
  • Host OS: Windows 7 (running on VirtualBox on my Mac)
  • Compiler: Clang
  • ABI: ARM, x86
  • STL: LLVM libc++
  • NDK API level: 26
  • Device API level: N/A (lib won't build, so can't build / deploy app that uses it)

ddgcodes avatar Dec 17 '17 05:12 ddgcodes

UPDATE:

Turns out if you setup the header include directories in just the right order, it works. The key seems to be that the libc++ header directories have to come before the Unified Headers directory. Then <cmath> references libc++'s version of <math.h>, which then references (and fixes) the Unified Headers <math.h>.

This is a really messy thing to do in Visual Studio, but that's on Microsoft to fix - I may file a ticket with them regarding that.

This blog post covers the issue, from GCC's perspective.

ddgcodes avatar Dec 18 '17 04:12 ddgcodes

Build sytem: Standalone toolchain (Visual Studio)

Is this actually a standalone toolchain, or something VS specific? As you say, yes, the libc++/libandroid_support/libc headers need to be in exactly the right order or it won't work.

This is known to be broken with standalone toolchains using GCC, FWIW. May be worth checking that VS really is using clang.

DanAlbert avatar Dec 18 '17 21:12 DanAlbert

I don't know exactly how Visual Studio does it; I just know it winds up invoking Clang (or GCC, you can choose) directly, passing it various arguments that you can customize. However, I think its base setup is wrong since it doesn't pass the proper include directories, let alone in the right order.

Does the Python script provided by the NDK (make_standalone_toolchain.py) handle setting up the include directories under the hood?

For reference, here are all the command line options Visual Studio passes to Clang when building a library in Debug mode for 32-bit ARM on my virtual machine (on which I have the NDK installed at C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r16b); you can see that the include directories are incomplete and in the wrong order:

-fpic "pch.h" -std=c++11 -fstack-protector "ARM\Debug\" -Wall -fno-strict-aliasing -I "C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r16b\platforms\android-26\arch-arm\usr\include" -I "C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r16b\sources\android\support\include" -I "C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r16b\sources\cxx-stl\llvm-libc++\include" -I "C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r16b\sources\cxx-stl\llvm-libc++abi\include" -g2 -gdwarf-2 -O0 -x c++-header -D "__ANDROID_API__=26" -fno-rtti -fno-omit-frame-pointer -mthumb -fexceptions -mfloat-abi=softfp -o "ARM\Debug\%(filename).o" -fno-short-enums

So from the looks of it, I should probably go report this as a bug to Microsoft.

ddgcodes avatar Dec 19 '17 06:12 ddgcodes

Does the Python script provided by the NDK (make_standalone_toolchain.py) handle setting up the include directories under the hood?

Yep. It Just Works:tm: for Clang.

So from the looks of it, I should probably go report this as a bug to Microsoft.

Yep, looks like it. I'll go ahead and close this.

DanAlbert avatar Dec 19 '17 06:12 DanAlbert