Search code examples
androidc++cmakeandroid-ndkclang

Android NDK: array file not found


I'm playing with Android NDK glfw android project.
https://github.com/xCuri0/glfw-android-example/tree/master/app/src/main/cpp.
and I tried to add:
include <array>
at the beginning of gl.c
But I got error:
fatal error: 'array' file not found

I've already added following lines to root CMakeLists:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-error=deprecated-declarations")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) 

build.gradle:

        externalNativeBuild {
            cmake {
                arguments("-DANDROID_STL=c++_shared")
                cppFlags ""
            }
        }

What could be the reason ?

Error msg:

/Users/renruisi/Library/Android/sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang --target=aarch64-none-linux-android31 --sysroot=/Users/renruisi/Library/Android/sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -DNDK_DEBUG=1 -Dglfw_example_EXPORTS -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/thirdparty/glfw/include -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/thirdparty/glfw/deps -I/Users/renruisi/Library/Android/sdk/ndk/26.1.10909125/sources/android/native_app_glue -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/. -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Audio -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Character -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Character/Inventory -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Character/Look -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Data -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/IO -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/IO/Components -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/IO/UITypes -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/IO/UITypes/Login -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Gameplay -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Gameplay/Combat -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Gameplay/MapleMap -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Gameplay/Physics -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Graphics -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Net -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Net/Handlers -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Net/Packets -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Template -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/src/Util -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/SYSTEM -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/thirdparty -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/thirdparty/asio/asio/include -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/thirdparty/bass/c -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/thirdparty/freetype/include -I/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/thirdparty/stb -I/Users/renruisi/Downloads/glfw-android-example/app/.cxx/Debug/295s6r6h/arm64-v8a/thirdparty/freetype/include -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security  -fno-limit-debug-info  -fPIC -MD -MT CMakeFiles/glfw-example.dir/gl.c.o -MF CMakeFiles/glfw-example.dir/gl.c.o.d -o CMakeFiles/glfw-example.dir/gl.c.o -c /Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/gl.c

/Users/renruisi/Downloads/glfw-android-example/app/src/main/cpp/gl.c:4:10: fatal error: 'array' file not found
#include <array>
         ^~~~~~~

Solution

  • The error you're encountering is because you're trying to include a C++ header () in a C file (gl.c). To fix this, either:

    1. Convert the file to C++: Rename gl.c to gl.cpp. This will signal the compiler to treat it as a C++ source file, allowing the use of C++ headers like .
    2. Remove C++ content: If the file must remain a C file, remove the inclusion of and any other C++-specific code or libraries.

    Make sure your build configuration is updated accordingly after these changes.