GPU delegate linker errors when building TFLite 2.16.1 for Android with CMake
Hi @eltimen,

Enabling GPU support requires additional libraries and dependencies related to OpenCL. This might be the reason for 
 the error ` tflite::gpu::gl and tflite::gpu::OptionalAndroidHardwareBuffer`.  Please try to add the following to the Android manifest in order to detect GPU delegate.
```
<uses-library android:name="libOpenCL.so"
           android:required="false"/>

<uses-library android:name="libOpenCL-pixel.so"
           android:required="false"/>
```
Also Verify that the NDK version (r25b) is compatible with the TensorFlow Lite version you are trying to build.

Thank You Hi @LakshmiKalaKadali,

I'm not using Gradle or Android Studio. I just tried to build `libtensorflow-lite.so` for Android using CMake following the [documentation](https://www.tensorflow.org/lite/guide/build_cmake#specifics_of_android_cross-compilation) and I got the linker errors above.

I tried to build it with Bazel as mentioned [here](https://www.tensorflow.org/lite/android/lite_build#set_up_build_environment_without_docker) and build was successful. According to [this page](https://www.tensorflow.org/lite/android/lite_build#configure_workspace_and_bazelrc) NDK r25b should be compatible with this TFLite version. 

According to the documentation, TensorFlow Lite supports both ways to build it for Android: with CMake and with Bazel. Looks like CMake building is broken with enabled GPU delegate in this version. This looks like a duplicate of https://github.com/tensorflow/tensorflow/issues/65744, investigating. Hi @eltimen, I was able to build fine with these steps:

```
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
cd tensorflow_src
git switch r2.16
cd ..
mkdir tflite_build
cd tflite_build
cmake -S ../tensorflow_src/tensorflow/lite -B build -D CMAKE_BUILD_TYPE=Release -D CMAKE_SYSTEM_PROCESSOR=aarch64 -D BUILD_SHARED_LIBS=ON -D TFLITE_ENABLE_GPU=ON -D CMAKE_SYSTEM_NAME=Linux -D CMAKE_SYSTEM_VERSION=29 -D ANDROID_PLATFORM=29 -D CMAKE_ANDROID_ARCH_ABI=arm64-v8a -D ANDROID_ABI=arm64-v8a -D CMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake
```

Please note $ANDROID_NDK is the path to my NDK, which is 25b.

I am building on MacOS, you didn't specify an OS so that could be an issue.

Can you please let us know what OS you are using? Additionally please review my reproduce steps and let me know if I did something different than what you are doing? Thanks. Hi @pkgoogle, I use Ubuntu 22.04.
I'm building TFLite from the tag `v2.16.1`, but you are building it from the branch `r2.16`.  
Can you try to reproduce this using the [release 2.16.1 version](https://github.com/tensorflow/tensorflow/releases/tag/v2.16.1)? Hi @eltimen, I am using a Debian system, and went to that tag, but am using NDK=r25c, I am still not reproducing the error:

```
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
cd tensorflow_src
git checkout v2.16.1
cd ..
mkdir tflite_build
cd tflite_build
cmake -S ../tensorflow_src/tensorflow/lite -B build -D CMAKE_BUILD_TYPE=Release -D CMAKE_SYSTEM_PROCESSOR=aarch64 -D BUILD_SHARED_LIBS=ON -D TFLITE_ENABLE_GPU=ON -D CMAKE_SYSTEM_NAME=Linux -D CMAKE_SYSTEM_VERSION=29 -D ANDROID_PLATFORM=29 -D CMAKE_ANDROID_ARCH_ABI=arm64-v8a -D ANDROID_ABI=arm64-v8a -D CMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake
```

It looks like my Clang is different (14.0.7) probably from the different r25c.

Can you try with a fresh repo, perhaps fresh NDK, and ensure you follow the same steps exactly? Let me know if somehow that resolves your issue. Hello @pkgoogle,
I followed your step exactly, the error does not come from the generating the build file but from actual building with `cmake --build . -j`, can you verify this is the correct command to build? Also just double checking, when building with CMake, are the steps in [this guide](https://www.tensorflow.org/lite/android/delegates/gpu_native) still needed to build GPU delegate? Hi @dorishyc3, I believe you are in the wrong thread, please follow your issue in that thread instead so we don't confuse the issues (Same error but different OS right now), to answer your question -- that is the correct command if you are following these directions: https://www.tensorflow.org/lite/guide/build_cmake. That guide is following directions with Bazel, I don't recommend you mix build systems unless you truly understand both and what they are doing. For `cmake` I would follow this: https://www.tensorflow.org/lite/guide/build_cmake#opencl_gpu_delegate. If you want to try Bazel or see if you can complete your workflow with it instead, please feel free to do so. This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. Hi, @pkgoogle. Sorry for the delay.

I tried building the TFLite exactly following your steps using NDK r25c, but I'm still reproducing this error. My CMake version is 3.27.7 Hi @eltimen,

It seems we have been miscommunicating slightly :). The original problem didn't include the `cmake --build . -j` step, so I wasn't doing that -- as seen in my previous comment. After doing that step, I was able to reproduce. To recompile to ensure there is no further miscommunication the reproduce steps are as follows:

```
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
cd tensorflow_src
git checkout v2.16.1
cd ..
mkdir tflite_build
cd tflite_build
cmake -S ../tensorflow_src/tensorflow/lite -B build -D CMAKE_BUILD_TYPE=Release -D CMAKE_SYSTEM_PROCESSOR=aarch64 -D BUILD_SHARED_LIBS=ON -D TFLITE_ENABLE_GPU=ON -D CMAKE_SYSTEM_NAME=Linux -D CMAKE_SYSTEM_VERSION=29 -D ANDROID_PLATFORM=29 -D CMAKE_ANDROID_ARCH_ABI=arm64-v8a -D ANDROID_ABI=arm64-v8a -D CMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake
cd build
cmake --build . -j
```

Hi @terryheo, can you please take a look? Thanks.
### Issue type

Build/Install

### Have you reproduced the bug with TensorFlow Nightly?

Yes

### Source

source

### TensorFlow version

2.16.1

### Custom code

Yes

### OS platform and distribution

_No response_

### Mobile device

_No response_

### Python version

_No response_

### Bazel version

_No response_

### GCC/compiler version

Clang 14.0.6 (Android NDK r25b)

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

I'm trying to build TFLite 2.16.1 for Android using CMake and Android NDK r25b and got the following linker errors related to GPU delegate. It looks like some source files was not added to CMake configuration files.

```
ld: error: undefined symbol: tflite::gpu::OptionalAndroidHardwareBuffer::OptionalAndroidHardwareBuffer()
>>> referenced by android_hardware_buffer.h:69 (tensorflow/lite/delegates/gpu/android_hardware_buffer.h:69)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(TfLiteGpuDelegateV2CreateAsync)
>>> referenced by android_hardware_buffer.h:69 (tensorflow/lite/delegates/gpu/android_hardware_buffer.h:69)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::RegisterBuffer(TfLiteOpaqueContext*, TfLiteIoType, TfLiteBackendBuffer const*, TfLiteAttributeMap const*, int))
>>> referenced by android_hardware_buffer.h:69 (tensorflow/lite/delegates/gpu/android_hardware_buffer.h:69)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::RegisterBuffer(TfLiteOpaqueContext*, TfLiteIoType, TfLiteBackendBuffer const*, TfLiteAttributeMap const*, int))
>>> referenced 5 more times

ld: error: undefined symbol: tflite::delegates::BackendAsyncKernelInterface::BackendAsyncKernelInterface()
>>> referenced by delegate.cc:687 (tensorflow/lite/delegates/gpu/delegate.cc:687)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::CreateAsyncRegistration()::$_2::__invoke(TfLiteContext*, char const*, unsigned long))
>>> did you mean: tflite::delegates::BackendAsyncKernelInterface::~BackendAsyncKernelInterface()
>>> defined in: CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o

ld: error: undefined symbol: tflite::delegates::utils::ReadBufferAttrs(TfLiteAttributeMap const*)
>>> referenced by delegate.cc:1048 (tensorflow/lite/delegates/gpu/delegate.cc:1048)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::RegisterBuffer(TfLiteOpaqueContext*, TfLiteIoType, TfLiteBackendBuffer const*, TfLiteAttributeMap const*, int))
>>> referenced by delegate.cc:911 (tensorflow/lite/delegates/gpu/delegate.cc:911)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::ReconcileRestrictions(TfLiteOpaqueContext const*, TfLiteOpaqueNode const*, int, TfLiteAttributeMap const*, TfLiteAttributeMap*, TfLiteAttributeMap*) const)

ld: error: undefined symbol: tflite::delegates::utils::WriteBufferAttrs(tflite::delegates::utils::BufferAttributes const&, TfLiteAttributeMap*)
>>> referenced by delegate.cc:913 (tensorflow/lite/delegates/gpu/delegate.cc:913)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::ReconcileRestrictions(TfLiteOpaqueContext const*, TfLiteOpaqueNode const*, int, TfLiteAttributeMap const*, TfLiteAttributeMap*, TfLiteAttributeMap*) const)
>>> referenced by delegate.cc:913 (tensorflow/lite/delegates/gpu/delegate.cc:913)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::ReconcileRestrictions(TfLiteOpaqueContext const*, TfLiteOpaqueNode const*, int, TfLiteAttributeMap const*, TfLiteAttributeMap*, TfLiteAttributeMap*) const)
>>> referenced by delegate.cc:913 (tensorflow/lite/delegates/gpu/delegate.cc:913)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::ReconcileRestrictions(TfLiteOpaqueContext const*, TfLiteOpaqueNode const*, int, TfLiteAttributeMap const*, TfLiteAttributeMap*, TfLiteAttributeMap*) const)
>>> referenced 2 more times

ld: error: undefined symbol: tflite::delegates::utils::ReadSyncAttrs(TfLiteAttributeMap const*)
>>> referenced by delegate.cc:936 (tensorflow/lite/delegates/gpu/delegate.cc:936)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::ReconcileRestrictions(TfLiteOpaqueContext const*, TfLiteOpaqueNode const*, int, TfLiteAttributeMap const*, TfLiteAttributeMap*, TfLiteAttributeMap*) const)
>>> referenced by delegate.cc:969 (tensorflow/lite/delegates/gpu/delegate.cc:969)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::SetAttributes(TfLiteOpaqueContext*, TfLiteOpaqueNode*, int, TfLiteAttributeMap const*))

ld: error: undefined symbol: tflite::delegates::utils::WriteSyncAttrs(tflite::delegates::utils::SyncAttributes const&, TfLiteAttributeMap*)
>>> referenced by delegate.cc:938 (tensorflow/lite/delegates/gpu/delegate.cc:938)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::ReconcileRestrictions(TfLiteOpaqueContext const*, TfLiteOpaqueNode const*, int, TfLiteAttributeMap const*, TfLiteAttributeMap*, TfLiteAttributeMap*) const)
>>> referenced by delegate.cc:940 (tensorflow/lite/delegates/gpu/delegate.cc:940)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::ReconcileRestrictions(TfLiteOpaqueContext const*, TfLiteOpaqueNode const*, int, TfLiteAttributeMap const*, TfLiteAttributeMap*, TfLiteAttributeMap*) const)

ld: error: undefined symbol: tflite::gpu::gl::WaitFdGpu(int)
>>> referenced by delegate.cc:1167 (tensorflow/lite/delegates/gpu/delegate.cc:1167)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::Eval(TfLiteOpaqueContext*, TfLiteOpaqueNode*, TfLiteExecutionTask*))

ld: error: undefined symbol: tflite::delegates::utils::WaitForAllFds(absl::lts_20230802::Span<int const>)
>>> referenced by delegate.cc:1170 (tensorflow/lite/delegates/gpu/delegate.cc:1170)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::Eval(TfLiteOpaqueContext*, TfLiteOpaqueNode*, TfLiteExecutionTask*))

ld: error: undefined symbol: tflite::gpu::gl::EglEnvironment::NewEglEnvironment(std::__ndk1::unique_ptr<tflite::gpu::gl::EglEnvironment, std::__ndk1::default_delete<tflite::gpu::gl::EglEnvironment> >*)
>>> referenced by delegate.cc:1175 (tensorflow/lite/delegates/gpu/delegate.cc:1175)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::Eval(TfLiteOpaqueContext*, TfLiteOpaqueNode*, TfLiteExecutionTask*))

ld: error: undefined symbol: tflite::delegates::utils::ConvertToTfLiteStatus(absl::lts_20230802::Status)
>>> referenced by delegate.cc:1175 (tensorflow/lite/delegates/gpu/delegate.cc:1175)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::Eval(TfLiteOpaqueContext*, TfLiteOpaqueNode*, TfLiteExecutionTask*))
>>> referenced by delegate.cc:1186 (tensorflow/lite/delegates/gpu/delegate.cc:1186)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::Eval(TfLiteOpaqueContext*, TfLiteOpaqueNode*, TfLiteExecutionTask*))
>>> referenced by delegate.cc:1187 (tensorflow/lite/delegates/gpu/delegate.cc:1187)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::Eval(TfLiteOpaqueContext*, TfLiteOpaqueNode*, TfLiteExecutionTask*))
>>> referenced 3 more times

ld: error: undefined symbol: tflite::gpu::gl::EglEnvironment::~EglEnvironment()
>>> referenced by memory:2427 (android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/memory:2427)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::Eval(TfLiteOpaqueContext*, TfLiteOpaqueNode*, TfLiteExecutionTask*))
>>> referenced by memory:2427 (android-ndk-r25b/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/memory:2427)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::Eval(TfLiteOpaqueContext*, TfLiteOpaqueNode*, TfLiteExecutionTask*))

ld: error: undefined symbol: tflite::gpu::AsyncBuffer::GetOpenGlBuffer(unsigned int&)
>>> referenced by delegate.cc:1186 (tensorflow/lite/delegates/gpu/delegate.cc:1186)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::Eval(TfLiteOpaqueContext*, TfLiteOpaqueNode*, TfLiteExecutionTask*))
>>> referenced by delegate.cc:1199 (tensorflow/lite/delegates/gpu/delegate.cc:1199)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::Eval(TfLiteOpaqueContext*, TfLiteOpaqueNode*, TfLiteExecutionTask*))

ld: error: undefined symbol: tflite::gpu::gl::CreateFdGpu()
>>> referenced by delegate.cc:1211 (tensorflow/lite/delegates/gpu/delegate.cc:1211)
>>>               CMakeFiles/tensorflow-lite.dir/delegates/gpu/delegate.cc.o:(tflite::gpu::(anonymous namespace)::DelegateAsyncKernel::Eval(TfLiteOpaqueContext*, TfLiteOpaqueNode*, TfLiteExecutionTask*))
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/tensorflow-lite.dir/build.make:6296: libtensorflow-lite.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:1336: CMakeFiles/tensorflow-lite.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
```

### Standalone code to reproduce the issue

```shell
CMake options:

cmake -S tensorflow/lite -B build -D CMAKE_BUILD_TYPE=Release -D CMAKE_SYSTEM_PROCESSOR=aarch64 -D BUILD_SHARED_LIBS=ON -D TFLITE_ENABLE_GPU=ON -D CMAKE_SYSTEM_NAME=Linux -D CMAKE_SYSTEM_VERSION=29 -D ANDROID_PLATFORM=29 -D CMAKE_ANDROID_ARCH_ABI=arm64-v8a -D ANDROID_ABI=arm64-v8a -D CMAKE_TOOLCHAIN_FILE=android-ndk-r25b/build/cmake/android.toolchain.cmake
```


### Relevant log output

_No response_