Search code examples
androidc++android-ndkc++17

Android NDK: Compilation Error with std::execution in C++17


I'm trying to compile a C++ program using the Android NDK that relies on the C++17 feature std::execution.

During the build process, I encounter the following errors:

error: no member named 'par' in namespace 'std::execution'
error: no member named 'par_unseq' in namespace 'std::execution'

Sample Program:

#include <execution>
#include <algorithm>
#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    std::for_each(std::execution::par, vec.begin(), vec.end(), [](int &n) { n *= 2; });
    return 0;
}

Compilation Command:

/Users/A.Chavez/Library/Android/sdk/ndk/27.0.11902837/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ \
  --target=aarch64-none-linux-android24 \
  --sysroot=/Users/A.Chavez/Library/Android/sdk/ndk/27.0.11902837/toolchains/llvm/prebuilt/darwin-x86_64/sysroot \
  -std=c++20 \
  -stdlib=libc++ \
  -I/Users/A.Chavez/Library/Android/sdk/ndk/27.0.11902837/sources/cxx-stl/llvm-libc++/include \
  -I/Users/A.Chavez/Library/Android/sdk/ndk/27.0.11902837/sources/cxx-stl/llvm-libc++abi/include \
  -I/Users/A.Chavez/Library/Android/sdk/ndk/27.0.11902837/sources/android/support/include \
  -c test.cpp

Context: I've set the APP_CPPFLAGS to -std=c++20 and APP_STL to c++_shared in my CMakeList.txt file. I am using NDK version 27.0.11902837 but this happens with any NDK that should support C++17

The documentation indicates that libc++ should support modern C++ features, but it seems std::execution might not be fully supported.

  1. How can I resolve this error and successfully compile my program using the Android NDK?
  2. Is there any specific configuration or additional steps required to enable support for std::execution?

I'm using CMake for building the project


Solution

  • Android NDK r27 RC 1 does not support parallel algorithms in libc++ and you can't use them in your code.

    clang-r522817/include/c++/v1/execution

    // <...>
    #include <__config>
    // <...>
    #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_PSTL) && _LIBCPP_STD_VER >= 17
    // <...>
    

    clang-r522817/include/c++/v1/__config

    // <...>
    #  if __has_feature(experimental_library)
    #    ifndef _LIBCPP_ENABLE_EXPERIMENTAL
    #      define _LIBCPP_ENABLE_EXPERIMENTAL
    #    endif
    #  endif
    // <...>
    #  if !defined(_LIBCPP_ENABLE_EXPERIMENTAL) && !defined(_LIBCPP_BUILDING_LIBRARY)
    #    define _LIBCPP_HAS_NO_INCOMPLETE_PSTL
    // <...>
    

    _LIBCPP_HAS_NO_INCOMPLETE_PSTL is defined. Hence, <execution> does not define execution policies.