Search code examples
c++android-ndklinker-errorsundefined-referenceandroid.mk

Cannot solve "undefined reference to" error building an Android NDK library


I have an "undefined reference to ___" error using the Android NDK. There are quite a few SO questions like mine, but none have helped me. My main problem may be understanding just what is wrong, or may be my ignorance of Android.mk makefile commands.

This is the very first output when I run ndk-build. Notice libProcessor.so, the function call to Image::findTransform() in Image.cpp, and of course the undefined reference to 'Matrix::Matrix(int, int)':

Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 15 in ./AndroidManifest.xml    
[armeabi-v7a] SharedLibrary  : libProcessor.so
/Applications/adt-bundle-mac-x86_64-20140702/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi-v7a/objs/Processor/Image.o: in function Image::findTransform(float, float, CGPoint, CGPoint, CGPoint, CGPoint, float&, float&, float&, float&, float&, float&, float&, float&):jni/Image.cpp:1230: error: undefined reference to 'Matrix::Matrix(int, int)'

The line of code it is complaining about is a plain:

    Matrix A(8,8);

That Matrix class in in a source code file, not a prebuilt library. I had compile errors for the Matrix class at first, but after including the newmat10.h file they all went away, so I know the newmat10 directory is being found and all its source code. So this is what is baffling me: the source code file that defines the Matrix class is being built into libProcessor.so right along with the Image.cpp code that calls it, so what else does it need to know to resolve the reference? Perhaps the problem is in my Android.mk file, which is here:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := Processor
LOCAL_SRC_FILES := Processor.cpp Image.cpp
LOCAL_LDLIBS    := -llog

LOCAL_C_INCLUDES := \
  $(LOCAL_PATH)/newmat10 \


include $(BUILD_SHARED_LIBRARY)

I especially need a fix for this error of course, but I'd also very much like to understand its specific nature. Is the system not saying, "I'm now trying to write the Image.o object file and I see the declaration of the Matrix object, but I do not know where I put the code for the Matrix constructor when I successfully compiled Image.cpp."?


Solution

  • So you don't have source files for Matrix included in the build. If you have several files, instead of listing them manually you can modify your Android.mk like this:

    LOCAL_SRC_FILES := Processor.cpp Image.cpp
    LOCAL_SRC_FILES += $(foreach file, $(wildcard jni/newmat10/*.cpp), ../${file})