How to link any library in ndk application

19,026

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/

Share:
19,026
Jeegar Patel
Author by

Jeegar Patel

Written first piece of code at Age 14 in HTML & pascal Written first piece of code in c programming language at Age 18 in 2007 Professional Embedded Software engineer (Multimedia) since 2011 Worked in Gstreamer, Yocto, OpenMAX OMX-IL, H264, H265 video codec internal, ALSA framework, MKV container format internals, FFMPEG, Linux device driver development, Android porting, Android native app development (JNI interface) Linux application layer programming, Firmware development on various RTOS system like(TI's SYS/BIOS, Qualcomm Hexagon DSP, Custom RTOS on custom microprocessors)

Updated on June 25, 2022

Comments

  • Jeegar Patel
    Jeegar Patel almost 2 years

    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