Search code examples
androidandroid-ndkjava-native-interface

Compiling with NDK gives error for successfully included header files


My app.cpp:

#include "app.h"


#include <EGL/egl.h>
#include <EGL/eglext.h>

void
Java_com_geek_hello_FilterStack_nativeEglSetFenceAndWait(JNIEnv* env, jobject thiz) {
  EGLDisplay display = eglGetCurrentDisplay();

  // Create a egl fence and wait for egl to return it.
  // Additional reference on egl fence sync can be found in:
  // http://www.khronos.org/registry/vg/extensions/KHR/EGL_KHR_fence_sync.txt
  EGLSyncKHR fence = eglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, NULL);
  if (fence == EGL_NO_SYNC_KHR) {
    return;
  }
  ...

When I run ndk-build, it fails finding methods present in egl.h and .so is not created. Here's the log:

app.cpp:31:72: error: 'eglCreateSyncKHR' was not declared in this scope 

followed by all KHR methods that belong to

Here's Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

#LOCAL_CFLAGS += -DEGL_EGLEXT_PROTOTYPES
LOCAL_CFLAGS = -Wno-psabi

LOCAL_SRC_FILES := app.cpp

#LOCAL_SHARED_LIBRARIES := libcutils libEGL

LOCAL_MODULE_TAGS := optional

LOCAL_MODULE := libapp
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2

include $(BUILD_SHARED_LIBRARY)

Solution

  • You need to add:

    #define EGL_EGLEXT_PROTOTYPES
    

    before your #include lines (well, specifically before including EGL/eglext.h).

    Also (and it looks like you're already doing this), you need to be building against at least API 14, since that's the first API level exposing this function in the public API.

    EDIT: or just uncomment the line in your Android.mk that reads:

    #LOCAL_CFLAGS += -DEGL_EGLEXT_PROTOTYPES