Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

android - How to link any library in ndk application

From this tutorial.

see i have one pre-built static library named as stackoverflow.a and it has stackoverflow.h

now i want to use the function of that static library in

ndk_demo.c // that tutorial has this file

for that inside ndk_demo.c i have added

#include 'stackoverflow.h'  

Edit

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

Now Application.mk

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

Now 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)

now 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)

Now from android-ndk-r7c directory i run

make APP=ndk_demo

it shows me error like

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.

why this happening i am not getting ?

if i comment

#include $(call all-subdir-makefiles)

this from jni/Android.mk then it shows following error

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

when you get "make: * No rule to make target `something.c'", it just means it can't find the file.

I'm a bit confused why you've organized your project like that, but if I was going to build your project, I would do it as follows:

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

Then I would use the following 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)

And the following:

Application.mk:

APP_MODULES := ndk_demo
APP_PLATFORM := android-8

Then finally, I would navigate to the directory (ndk_demo) and run ndk-build.

ndk-build is android's build tool. You should use it. It can be found at:

(AndroidSDK)/(NDK)/ndk-build

if you are using windows, you will either have to type the full path of ndk-build into the console, or add an environment variable to your system before trying to run it.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...