Search code examples
androidc++android-ndkinclude-path

Why are include paths unseen when #include in Android NDK code?


I want to add an include path to my Android NDK project but the included files are not found by the compiler. I double checked that the included files exist on my system. What am I doing wrong here?

Android.mk:

include $(CLEAR_VARS)

LOCAL_MODULE    := hello-jni
LOCAL_CFLAGS := -Wno-write-strings
LOCAL_C_INCLUDES +:= $(LOCAL_PATH)
LOCAL_CFLAGS +:= -O3 
LOCAL_CPPFLAGS +:=$(LOCAL_CFLAGS)
LOCAL_CFLAGS := -I/absolute/path/to/my/include //--->here the include path is set
LOCAL_SRC_FILES := hello-jni.cpp
LOCAL_LDLIBS := -ldl -llog -lc

include $(BUILD_SHARED_LIBRARY)

Included headers in jni/hello-jni.cpp:

extern "C" {
#include <codecs/codecs.h>
#include <test/test.h>
#include <mymath/mymath.h>
}
....

Error log:

jni/hello-jni.cpp:9:34: fatal error: codecs/codecs.h: No such file or directory
compilation terminated.
make: *** [obj/local/armeabi/objs/hello-jni/hello-jni.o] Error 1

Solution

  • I think the problem is this line:

    LOCAL_CPPFLAGS +:=$(LOCAL_CFLAGS)
    

    Using +:= here means that the value of LOCAL_CFLAGS used is that at the time of the declaration of LOCAL_CPPFLAGS. At this point LOCAL_CFLAGS has not been set (it is set on the next line). Try changing it to:

    LOCAL_CPPFLAGS +=$(LOCAL_CFLAGS)
    

    This will evaluate LOCAL_CPPFLAGS every time it is used and will pick up the right value. Alternatively, declare LOCAL_CFLAGS before you use it in LOCAL_CPPFLAGS.