如何链接ndk应用程序中的任何库

时间:2012-04-11 13:32:15

标签: android c makefile android-ndk java-native-interface

来自this tutorial

看到我有一个名为stackoverflow.a的预建静态库,它有stackoverflow.h

现在我想在

中使用该静态库的功能

ndk_demo.c //该教程有此文件

ndk_demo.c里面我添加了

#include 'stackoverflow.h'  

修改

inside `android-ndk-r7c`
         |
        apps
         |
        ndk_demo
         |
     -----------------
     |               |
   project          Application.mk
     |
--------------------
|                   |
all other           |
folder             jni
                    |  
-------------------------------------------------------------------
  |               |                     |          |              |
ndk_demo.c      stackoverflow.h        lib    com_marakana       Android.mk
                                        |      _NativeLib.h
                                        |
                             --------------------
                             |                   |
                          Android.mk          libstackoverflow.a

现在 Application.mk

APP_PROJECT_PATH := $(call my-dir)/project
APP_MODULES      := ndk_demo stackover

现在 jni / Android.mk

include $(call all-subdir-makefiles)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := ndk_demo
LOCAL_SRC_FILES := ndk_demo.c
LOCAL_STATIC_LIBRARIES := stackover
include $(BUILD_SHARED_LIBRARY)

现在 jni / lib / Android.mk

   LOCAL_PATH := $(call my-dir)
   include $(CLEAR_VARS)
   LOCAL_MODULE := stackover
   LOCAL_SRC_FILES := libstackoverflow.a
   include $(PREBUILT_STATIC_LIBRARY)
   LOCAL_PATH := $(call my-dir)

现在来自android-ndk-r7c目录 我跑

make APP=ndk_demo

它显示我的错误

Android NDK: Building for application 'ndk_demo'    
make: *** No rule to make target `build/core/ndk_demo.c', needed by `out/apps/ndk_demo/armeabi/objs/ndk_demo/ndk_demo.o'.  Stop.

为什么会发生这种情况我没有得到?

如果我发表评论

#include $(call all-subdir-makefiles)

这来自jni/Android.mk,然后显示以下错误

Android NDK: Building for application 'ndk_demo'    
Compile thumb  : ndk_demo <= ndk_demo.c
SharedLibrary  : libndk_demo.so
./out/apps/ndk_demo/armeabi/objs/ndk_demo/ndk_demo.o: In function `Java_com_marakana_NativeLib_hello':
/home/jeegar/android-ndk-r7c/apps/ndk_demo/project/jni/ndk_demo.c:10: undefined reference to `stackoverflowInit'
collect2: ld returned 1 exit status
make: *** [out/apps/ndk_demo/armeabi/libndk_demo.so] Error 1

1 个答案:

答案 0 :(得分:5)

当你得到“make: * 没有规则来制作目标`something.c'”时,它只是意味着找不到文件。

我有点困惑为什么你这样组织你的项目,但如果我打算建立你的项目,我会这样做:

(doesn't matter which directory)
|
-->(ndk_demo)
-->-->(jni)
-->-->-->Application.mk
-->-->-->Android.mk
-->-->-->com_marakana_NativeLib.h
-->-->-->ndk_demo.c
-->-->(stackoverflow)
-->-->-->stackoverflow.h
-->-->-->libstackoverflow.a

然后我将使用以下makefile:

<强> Android.mk:

LOCAL_PATH := $(call my-dir)

### include stackoverflow as a prebuilt lib ###

include $(CLEAR_VARS)

LOCAL_MODULE            := stackoverflow-prebuilt
LOCAL_SRC_FILES         := ../stackoverflow/libstackoverflow.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../stackoverflow

include $(PREBUILT_STATIC_LIBRARY)

### build your ndk lib ###

include $(CLEAR_VARS)

LOCAL_MODULE := ndk_demo
LOCAL_C_INCLUDES := $(LOCAL_PATH) \
                    $(LOCAL_PATH)/../stackoverflow
LOCAL_SRC_FILES := ndk_demo.c

LOCAL_LDLIBS := -llog
LOCAL_STATIC_LIBRARIES := stackoverflow-prebuilt

include $(BUILD_SHARED_LIBRARY)

以下内容:

<强> Application.mk:

APP_MODULES := ndk_demo
APP_PLATFORM := android-8

最后,我会导航到目录(ndk_demo)并运行ndk-build。

ndk-build是android的构建工具。你应该使用它。它可以在:

找到
(AndroidSDK)/(NDK)/ndk-build

如果您使用的是Windows,则必须在控制台中键入ndk-build的完整路径,或者在尝试运行之前将环境变量添加到系统中。

http://www.windows7hacker.com/index.php/2010/05/how-to-addedit-environment-variables-in-windows-7/