Search code examples
androidandroid-ndkandroid-ndk-r5

Linking static libraries in android ndk


I'm trying to compile the nvfile library from http://developer.nvidia.com/tegra-resources, in the android ndk sample, libs folder. Anyway, as I don't really need the entire suite of libraries, I pulled out the one that I needed, with what appeared to be it's dependencies. Here is the Android.mk file for compiling them.

include $(CLEAR_VARS)
LOCAL_MODULE     := nvthread
LOCAL_CFLAGS     := -Wall -g
LOCAL_LDFLAGS    := -Wl,-Map,xxx.map
LOCAL_SRC_FILES  := nv/nv_thread/nv_thread.c
LOCAL_C_INCLUDES := nv

include $(BUILD_STATIC_LIBRARY)


include $(CLEAR_VARS)
LOCAL_MODULE           := nvfile
LOCAL_CFLAGS           := -Wall -g
LOCAL_LDFLAGS          := -Wl,-Map,xxx.map
LOCAL_SRC_FILES        := nv/nv_file/nv_file.c
LOCAL_C_INCLUDES       := nv
LOCAL_STATIC_LIBRARIES := nvthread nvapkfile

include $(BUILD_STATIC_LIBRARY)


include $(CLEAR_VARS)
LOCAL_MODULE           := nvapkfile
LOCAL_CFLAGS           := -Wall -g
LOCAL_LDFLAGS          := -Wl,-Map,xxx.map
LOCAL_SRC_FILES        := nv/nv_apk_file/nv_apk_file.c
LOCAL_C_INCLUDES       := nv
LOCAL_STATIC_LIBRARIES := nvthread

include $(BUILD_STATIC_LIBRARY)

The nvapkfile library seems to be able to link just fine with nvthread, but the nvfile library doesn't seem to want to link to the nvapkfile library at all. The include files in the source code are working properly, it's just that whenever I try to compile it, I get an undefined reference. Here is a sample of output:

/home/leif/MarbleMachine/android/obj/local/armeabi/libnvfile.a(nv_file.o): In function `NvFInit':
/home/leif/MarbleMachine/android/jni/nv/nv_file/nv_file.c:49: undefined reference to `NvAPKInit'
/home/leif/MarbleMachine/android/obj/local/armeabi/libnvfile.a(nv_file.o): In function `NvFOpen':
/home/leif/MarbleMachine/android/jni/nv/nv_file/nv_file.c:77: undefined reference to `NvAPKOpen'
/home/leif/MarbleMachine/android/jni/nv/nv_file/nv_file.c:82: undefined reference to `NvAPKOpen'
/home/leif/MarbleMachine/android/obj/local/armeabi/libnvfile.a(nv_file.o): In function `NvFClose':
/home/leif/MarbleMachine/android/jni/nv/nv_file/nv_file.c:97: undefined reference to `NvAPKClose'
/home/leif/MarbleMachine/android/obj/local/armeabi/libnvfile.a(nv_file.o): In function `NvFGetc':
/home/leif/MarbleMachine/android/jni/nv/nv_file/nv_file.c:118: undefined reference to `NvAPKGetc'
/home/leif/MarbleMachine/android/obj/local/armeabi/libnvfile.a(nv_file.o): In function `NvFGets':
/home/leif/MarbleMachine/android/jni/nv/nv_file/nv_file.c:133: undefined reference to `NvAPKGets'
/home/leif/MarbleMachine/android/obj/local/armeabi/libnvfile.a(nv_file.o): In function `NvFSize':
/home/leif/MarbleMachine/android/jni/nv/nv_file/nv_file.c:148: undefined reference to `NvAPKSize'
/home/leif/MarbleMachine/android/obj/local/armeabi/libnvfile.a(nv_file.o): In function `NvFSeek':
/home/leif/MarbleMachine/android/jni/nv/nv_file/nv_file.c:171: undefined reference to `NvAPKSeek'
/home/leif/MarbleMachine/android/obj/local/armeabi/libnvfile.a(nv_file.o): In function `NvFTell':
/home/leif/MarbleMachine/android/jni/nv/nv_file/nv_file.c:186: undefined reference to `NvAPKTell'
/home/leif/MarbleMachine/android/obj/local/armeabi/libnvfile.a(nv_file.o): In function `NvFRead':
/home/leif/MarbleMachine/android/jni/nv/nv_file/nv_file.c:201: undefined reference to `NvAPKRead'
/home/leif/MarbleMachine/android/obj/local/armeabi/libnvfile.a(nv_file.o): In function `NvFEOF':
/home/leif/MarbleMachine/android/jni/nv/nv_file/nv_file.c:216: undefined reference to `NvAPKEOF'

I haven't modified the actual c or h files at all. But for reference, is here a piece of the relative C file in question:

#include "nv_file.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef ANDROID
#include "../nv_apk_file/nv_apk_file.h"
#define SUPPORT_APK 1
#include <unistd.h>
#endif

//...

void  NvFInit()
{
#ifdef SUPPORT_APK
    NvAPKInit();
#endif
}

This is lines 22-32 and 46-51 of nv_file.c

As you can see, the header is getting included, but it's not linking. Does anyone have any idea what I'm missing here? Thank you.


Solution

  • Ugg..it appears I just had it in the wrong order. Turning this line (in the nvfile module):

    LOCAL_STATIC_LIBRARIES := nvthread nvapkfile
    

    to

    LOCAL_STATIC_LIBRARIES := nvapkfile nvthread
    

    made it compile just fine. Can anyone confirm that the order you list libraries in LOCAL_STATIC_LIBRARIES can be important? Thanks.