在Android NDK中链接静态库

8

我正在尝试编译来自http://developer.nvidia.com/tegra-resources的nvfile库,在Android NDK示例中的libs文件夹中。然而,由于我实际上并不需要整套库,因此我提取了一个我需要的库,以及其依赖项。以下是用于编译它们的Android.mk文件。

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)

nvapkfile库似乎可以与nvthread正常链接,但nvfile库似乎根本不想链接到nvapkfile库。源代码中的包含文件正常工作,只是每当我尝试编译它时,我会得到一个未定义的引用。以下是输出示例:

/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'

我并没有实际修改c或h文件。但是为了参考,这里是相关C文件的一部分:

#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
}

这是nv_file.c的22-32行和46-51行。

可以看到,头文件被包含进来了,但是没有链接。这里缺少什么东西吗?谢谢。

1个回答

4

哦,看来我只是顺序搞错了。调整一下 nvfile 模块中这行代码的顺序:

LOCAL_STATIC_LIBRARIES := nvthread nvapkfile

to

LOCAL_STATIC_LIBRARIES := nvapkfile nvthread

我已经成功编译了它。有人可以确认在LOCAL_STATIC_LIBRARIES中列出库的顺序是否很重要吗?谢谢。


2
我认为你刚刚确认了这可能很重要。它是否应该重要是另一回事(我没有答案)。 - cidermonkey
1
这个消息支持顺序的重要性,以及为什么它很重要。https://groups.google.com/d/msg/android-ndk/az5uLxh04OI/xb5WWzD88msJ - Kekoa
1
不幸的是,我也曾经通过艰苦的方式发现了这一点,我可以确认顺序很重要,尽管我不得不试着玩弄它才能让它正常工作。请参见https://code.google.com/p/android/issues/detail?id=39378以获取更多相关讨论,以及此处:http://osdir.com/ml/android-ndk/2010-10/msg00018.html。 - Mick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接