Android: CMake fails to find basic NDK headers when compiling with standalone toolchain - bug, or I'm missing something?

I’m trying to compile a library for Android that depends on EGL. I have NDK installed in C:\Microsoft\AndroidNDK\android-ndk-r23c and SDK installed in C:\Microsoft\AndroidSDK.

However, find_package(OpenGL) fails with:

-- Could NOT find OpenGL (missing: OPENGL_opengl_LIBRARY OPENGL_glx_LIBRARY OPENGL_INCLUDE_DIR)
-- Could NOT find OPENGLES2 (missing: OPENGLES2_INCLUDE_DIR)
-- Could NOT find OPENGLES1 (missing: OPENGLES1_INCLUDE_DIR)

My CMake command:

cmake .. -DCMAKE_ANDROID_STANDALONE_TOOLCHAIN=C:/Microsoft/AndroidNDK/android-ndk-r23c/build/cmake/android.toolchain.cmake -A ARM64 -G "Visual Studio 17 2022" -DCMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH=FALSE --debug-find 2> 0

As you can notice, I’m disabling CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH, because otherwise find_package searches in folders in PATH environment variable, and I don’t want that (also, it pollutes --debug-find logs a lot).

Here’s --debug-find output, and that’s where things get interesting:

Make Debug Log at C:/Program Files/CMake/share/cmake-3.27/Modules/FindOpenGL.cmake:265 (find_path):
  find_path called with the following settings:

    VAR: OPENGL_EGL_INCLUDE_DIR
    NAMES: "EGL/egl.h"
    Documentation: Path to a file.
    Framework
      Only Search Frameworks: 0
      Search Frameworks Last: 0
      Search Frameworks First: 0
    AppBundle
      Only Search AppBundle: 0
      Search AppBundle Last: 0
      Search AppBundle First: 0
    CMAKE_FIND_USE_CMAKE_PATH: 1
    CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 0
    CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 0
    CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 1
    CMAKE_FIND_USE_INSTALL_PREFIX: 1

  find_path considered the following locations:

    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/21/include/aarch64-linux-android/EGL/egl.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/21/include/EGL/egl.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/21/EGL/egl.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/Microsoft/AndroidNDK/android-ndk-r23c/sysroot/usr/include/EGL/egl.h

  The item was not found. 

egl.h actually exists at “c:\Microsoft\AndroidNDK\android-ndk-r23c\toolchains\llvm\prebuilt\windows-x86_64\sysroot\usr\include\EGL\egl.h”. However, CMake insists on appending CMAKE_SYSROOT to EVERY search path - including, for some reason, sysroot itself. I THINK it’s a bug (maybe 3.27.0-specific one?), but maybe I’m missing something?

Thanks for trying the release candidate. This may be CMake Issue 25004. Please try 3.27.0-rc3 or later.

If it still happens with that, please also try 3.26 and perhaps a few earlier releases to see if this was an older regression.

Thank you for your reply. I tried rc4, and now I can’t even get past compiler detection. Then I re-read the docs again, and I think I was doing something wrong before: I should not be using CMAKE_ANDROID_STANDALONE_TOOLCHAIN if I want to get a Visual Studio project, as that doesn’t seem to be supported. However, if I try to generate a project using CMAKE_ANDROID_TOOLCHAIN, it also doesn’t work (with the same errors as https://gitlab.kitware.com/cmake/cmake/-/issues/24290).

I’m trying to understand if this is NDK, Visual Studio, or CMake issue. Running cmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=C:/Microsoft/AndroidNDK/android-ndk-r23c/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a produces the following output:

CMake Warning (dev) at CMakeLists.txt:20 (project):
  cmake_minimum_required() should be called prior to this top-level project()
  call.  Please see the cmake-commands(7) manual for usage documentation of
  both commands.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- ANDROID_PLATFORM not set. Defaulting to minimum supported version
16.
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.27/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: 'I:/dd2_reborn/allegro.git/build_ndr/CMakeFiles/CMakeScratch/TryCompile-oqcq6v'

    Run Build Command(s): "C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe" cmTC_56b2a.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=17.0 /v:n
    MSBuild version 17.6.3+07e294721 for .NET Framework
    Build started 02.07.2023 19:48:15.

    Project "I:\dd2_reborn\allegro.git\build_ndr\CMakeFiles\CMakeScratch\TryCompile-oqcq6v\cmTC_56b2a.vcxproj" on node 1 (default targets).
    SetBuildDefaultEnvironmentVariables:
      ANDROID_HOME=C:\ProgramData\Microsoft\AndroidSDK\25
      ANDROID_SDK_ROOT=C:\ProgramData\Microsoft\AndroidSDK\25
      ANT_HOME=
      JAVA_HOME=C:\Program Files\Android\jdk\jdk-8.0.302.8-hotspot\jdk8u302-b08
      NDK_ROOT=C:\\Microsoft\AndroidNDK\android-ndk-r23c
    PrepareForBuild:
      Creating directory "cmTC_56b2a.dir\Debug\".
      Creating directory "I:\dd2_reborn\allegro.git\build_ndr\CMakeFiles\CMakeScratch\TryCompile-oqcq6v\Debug\".
      Creating directory "cmTC_56b2a.dir\Debug\cmTC_56b2a.tlog\".
    InitializeBuildStatus:
      Creating "cmTC_56b2a.dir\Debug\cmTC_56b2a.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
    ClCompile:
      C:\\Microsoft\AndroidNDK\android-ndk-r23c\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe -c -fdiagnostics-format=msvc --gcc-toolchain="C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\x86_64-4.9\\prebuilt\\windows-x86_64" -target "x86_64-none-linux-android1" --sysroot="C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot" -isystem "C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\include\\x86_64-linux-android" -isystem "C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\lib64\\clang\\12.0.9\\include" -isystem "C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\sources\\android\\support\\include" -isystem "C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\include" -I "C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\sources\\cxx-stl\\llvm-libc++\\include" -I "C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\sources\\cxx-stl\\llvm-libc++abi\\include" -I "C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\include" -I "C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\include\\x86_64-linux-android" -I "C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\sources\\android\\support\\include" -I "C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\lib64\\clang\\12.0.9\\include" -gline-tables-only -o "cmTC_56b2a.dir\\Debug\\testCCompiler.o" -Wall -O3 -fno-strict-aliasing -fomit-frame-pointer -fno-exceptions -ffunction-sections -fdata-sections -fstack-protector -fpic -fno-short-enums -D __ANDROID_API__=1 -D NDEBUG -D ANDROID -D _FORTIFY_SOURCE=2 -D "CMAKE_INTDIR=\"Debug\"" -x c  -g -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wformat -Werror=format-security -fno-limit-debug-info "I:\\dd2_reborn\\allegro.git\\build_ndr\\CMakeFiles\\CMakeScratch\\TryCompile-oqcq6v\\testCCompiler.c"
      testCCompiler.c
      In file included from <built-in>:352:
    <command line>(1,9): warning : '__ANDROID_API__' macro redefined [-Wmacro-redefined] [I:\dd2_reborn\allegro.git\build_ndr\CMakeFiles\CMakeScratch\TryCompile-oqcq6v\cmTC_56b2a.vcxproj]
      #define __ANDROID_API__ 1
              ^
      <built-in>(345,9): note: previous definition is here
      #define __ANDROID_API__ __ANDROID_MIN_SDK_VERSION__
              ^
      1 warning generated.
    Link:
      C:\\Microsoft\AndroidNDK\android-ndk-r23c\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe -fdiagnostics-format=msvc -o"cmTC_56b2a.dir\\Debug\\cmTC_56b2a." -Wl,-soname="cmTC_56b2a." -Wl,-rpath-link="C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\lib\\x86_64-linux-android" -Wl,-rpath-link="C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\lib\\x86_64-linux-android\\1" -Wl,-L"C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\lib\\x86_64-linux-android" -Wl,-L"C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\lib\\x86_64-linux-android\\1" -Wl,-L"C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\x86_64-4.9\\prebuilt\\windows-x86_64\\lib\\gcc\\x86_64-linux-android\\4.9.x" -Wl,-L"C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\sources\\cxx-stl\\llvm-libc++\\libs\\x86_64" -Wl,--no-undefined --gcc-toolchain="C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\x86_64-4.9\\prebuilt\\windows-x86_64" -target "x86_64-none-linux-android1" --sysroot="C:\\\\Microsoft\\AndroidNDK\\android-ndk-r23c\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot" -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -shared  -static-libstdc++ -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,--gc-sections cmTC_56b2a.dir\\Debug\\testCCompiler.o "-latomic" "-lm" -lc++_static -lc++abi -llog -landroid
      ld: error: cannot open crtbegin_so.o: No such file or directory
      ld: error: unable to find library -llog
      ld: error: unable to find library -landroid
      ld: error: cannot open crtend_so.o: No such file or directory
      clang: error: linker command failed with exit code 1 (use -v to see invocation)
    C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Application Type\Android\3.0\Android.Common.targets(125,5): error MSB6006: "clang.exe" exited with code 1. [I:\dd2_reborn\allegro.git\build_ndr\CMakeFiles\CMakeScratch\TryCompile-oqcq6v\cmTC_56b2a.vcxproj]
    Done Building Project "I:\dd2_reborn\allegro.git\build_ndr\CMakeFiles\CMakeScratch\TryCompile-oqcq6v\cmTC_56b2a.vcxproj" (default targets) -- FAILED.

    Build FAILED.

    "I:\dd2_reborn\allegro.git\build_ndr\CMakeFiles\CMakeScratch\TryCompile-oqcq6v\cmTC_56b2a.vcxproj" (default target) (1) ->
    (ClCompile target) ->
      <command line>(1,9): warning : '__ANDROID_API__' macro redefined [-Wmacro-redefined] [I:\dd2_reborn\allegro.git\build_ndr\CMakeFiles\CMakeScratch\TryCompile-oqcq6v\cmTC_56b2a.vcxproj]


    "I:\dd2_reborn\allegro.git\build_ndr\CMakeFiles\CMakeScratch\TryCompile-oqcq6v\cmTC_56b2a.vcxproj" (default target) (1) ->
    (Link target) ->
      C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Application Type\Android\3.0\Android.Common.targets(125,5): error MSB6006: "clang.exe" exited with code 1. [I:\dd2_reborn\allegro.git\build_ndr\CMakeFiles\CMakeScratch\TryCompile-oqcq6v\cmTC_56b2a.vcxproj]

        1 Warning(s)
        1 Error(s)

    Time Elapsed 00:00:01.45





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:20 (project)


-- Configuring incomplete, errors occurred!

The gist of the problem seems to be the wrong triple for target architecture: the compiler check uses “sysroot\usr\lib\x86_64-linux-android” where actually it should use “sysroot\usr\lib\aarch64-linux-android”. Which is strange, because if I look at CMakeConfigureLog.yaml log, I see:

      The target system is: Android - 1 - aarch64
      The host system is: Windows - 10.0.19043 - AMD64

So it correctly detects aarch64 (from my ANDROID_ABI)… And proceeds to ignore it and try to use x86_64, and the fails to find system libraries.

cmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=C:/Microsoft/AndroidNDK/android-ndk-r23c/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a

I don’t believe the Visual Studio generator supports toolchain files. Could you try using Ninja? Our project is currently building against the NDK just fine with 3.27 (Granted we are on Linux).

Here is what our CI looks like:

cmake -S . -B build/ -G "Ninja" \
--toolchain $ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake \
-D ANDROID_PLATFORM=26 \
-D CMAKE_ANDROID_ARCH_ABI=arm64-v8a

It says in the docs (https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-android-with-the-ndk): “A toolchain file may configure Makefile Generators, Ninja Generators, or Visual Studio Generators to target Android for cross-compiling.”, so VS should work, no?

Anyway, I don’t have Ninja installed, but with -G"NMake Makefiles" I can get past compiler checks, and back to the original problem of broken search paths. With

cmake .. -G "NMake Makefiles" ^
-DCMAKE_TOOLCHAIN_FILE=C:/Microsoft/AndroidNDK/android-ndk-r23c/build/cmake/android.toolchain.cmake ^
-DANDROID_ABI=arm64-v8a ^
-DANDROID_PLATFORM=25 ^
-DANDROID_TOOLCHAIN=aarch64-linux-android-25 ^
-DCMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH=FALSE ^
--debug-find 2>0

I get:

    VAR: OPENGL_GLES2_INCLUDE_DIR
    NAMES: "GLES2/gl2.h"
    Documentation: Path to a file.
    Framework
      Only Search Frameworks: 0
      Search Frameworks Last: 0
      Search Frameworks First: 0
    AppBundle
      Only Search AppBundle: 0
      Search AppBundle Last: 0
      Search AppBundle First: 0
    CMAKE_FIND_USE_CMAKE_PATH: 1
    CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
    CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 0
    CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 1
    CMAKE_FIND_USE_INSTALL_PREFIX: 1

  find_path considered the following locations:

    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/lib/aarch64-linux-android/24/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/lib/aarch64-linux-android/24/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/lib/aarch64-linux-android/24/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/sysroot/usr/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/24/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/24/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/24/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/Microsoft/AndroidNDK/android-ndk-r23c/sysroot/usr/include/GLES2/gl2.h

  The item was not found.

CMake again appends Microsoft\AndroidNDK\android-ndk-r23c twice and goes looking in C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/Microsoft/AndroidNDK/android-ndk-r23c/sysroot/usr/include/GLES2/ instead of c:\Microsoft\AndroidNDK\android-ndk-r23c\toolchains\llvm\prebuilt\windows-x86_64\sysroot\usr\include\GLES2\

I tried CMake 3.26 and it at least manages to find GLES2 with NMake Makefiles generator. With “Visual Studio 17 2022” generator. CMake 3.26 still fails basic compiler checks, as in the issue I already linked above (ld: error: cannot open crtbegin_so.o: No such file or directory etc).

There is definitely a regression of some kind in find_path between 3.26 and 3.27.rcX. Here’s the list of paths 3.26 searches for GLES2, to compare with 3.27.rc4 in my message to buildSystemPerson above:

CMake Debug Log at cmake/FindOpenGLES2.cmake:13 (find_path):
  find_path called with the following settings:

    VAR: OPENGLES2_INCLUDE_DIR
    NAMES: "GLES2/gl2.h"
    Documentation: Path to a file.
    Framework
      Only Search Frameworks: 0
      Search Frameworks Last: 0
      Search Frameworks First: 0
    AppBundle
      Only Search AppBundle: 0
      Search AppBundle Last: 0
      Search AppBundle First: 0
    CMAKE_FIND_USE_CMAKE_PATH: 1
    CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: 1
    CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: 0
    CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 1
    CMAKE_FIND_USE_INSTALL_PREFIX: 1

  find_path considered the following locations:

    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/lib/aarch64-linux-android/24/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/lib/aarch64-linux-android/24/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/lib/aarch64-linux-android/24/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/local/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/local/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/local/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/Program Files/CMake/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/Program Files/CMake/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/Program Files/CMake/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/Program Files (x86)/ALLEGRO/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/Program Files (x86)/ALLEGRO/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/Program Files (x86)/ALLEGRO/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/X11R6/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/X11R6/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/X11R6/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/pkg/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/pkg/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/pkg/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/opt/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/opt/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/opt/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/usr/include/X11/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/24/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/24/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/lib/aarch64-linux-android/24/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/local/include/aarch64-linux-android/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/local/include/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/local/GLES2/gl2.h
    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/aarch64-linux-android/GLES2/gl2.h

  The item was found at

    C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/GLES2/gl2.h

Is the NDK you are trying to use the one installed as part of Visual Studio, or are you trying to use one you’ve installed yourself? If the latter, please try switching to a VS-provided NDK and see if that works. My personal notes indicate using NDK with Visual Studio generators should work with CMake 3.19 or later, but also that using the VS-provided NDK may be more reliable than a separately installed NDK (unfortunately, I didn’t record why).

Craig, I’m indeed using VS-provided NDK.

I’m closer to getting Visual Studio generator working:

cmake .. -G "Visual Studio 17 2022" -A ARM64 ^
-DCMAKE_TOOLCHAIN_FILE=C:/Microsoft/AndroidNDK/android-ndk-r23c/build/cmake/android.toolchain.cmake ^
-DANDROID_ABI=arm64-v8a ^
-DANDROID_PLATFORM=25 ^
-DNDK_ROOT=C:/Microsoft/AndroidNDK/android-ndk-r23c/ ^

This way, I get ALMOST correct path for libraries. Unfortunately, it’s still incorrect, because it appends ANDROID_API 1 instead of what I pass through ANDROID_PLATFORM.

As far as I understand, the problem goes like this: NDK’s toolchain forces CMAKE_SYSTEM_VERSION to 1, and CMake uses CMAKE_SYSTEM_VERSION to generate Visual Studio files and set AndroidAPILevel variable.

I remain baffled. Is NDK toolchain + VS generator supposed to work? If so, how can I pass the correct API level to generator? If not, then what’s supposed to work? Is there any way to generate Adnroid project for Visual Studio when not using Tegra-Android?

Platform/Android.cmake says:

"# Commonly used Android toolchain files that pre-date CMake upstream support
# set CMAKE_SYSTEM_VERSION to 1.  Avoid interfering with them."

But what IS “CMake upstream support” in this context?

OK. Solved this, works with CMake 3.26. I just might be an idiot :slight_smile: It seems all I needed was:

cmake .. -G "Visual Studio 17 2022" -A ARM64 ^
-DCMAKE_SYSTEM_NAME=Android ^
-DCMAKE_ANDROID_NDK=C:/Microsoft/AndroidNDK/android-ndk-r23c ^
-DCMAKE_SYSTEM_VERSION=26

In my defense, the documentation says

A toolchain file may configure cross-compiling for Android by setting the CMAKE_SYSTEM_NAME variable to Android. Further configuration is specific to the Android development environment to be used.

For Visual Studio Generators, CMake expects NVIDIA Nsight Tegra Visual Studio Edition or the Visual Studio tools for Android to be installed. See those sections for further configuration details.

But the the link for Visual Studio tools for Android goes to NDK toolchain section, and there is NO section about this, so I got confused, and thought I need NDK toolchain to build for Visual Studio. Incidentally, the section about NDK toolchain clearly says you can use it to generate Visual Studio projects, but apparently that does not work well (or at all) for reasons described above… I think the docs need a clarification.

Oh, and -A ARM64 still leads to armeabi-v7a instead of arm64-v8a, so I’m still missing a small piece, but that’s less of a problem than everything else.