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
214 views
in Technique[技术] by (71.8m points)

android - Can't create shared library with static inside

Please help I search throw whole internet but I can't find answer ...

C Layer

I have created simple function int mean(int, int); and place it in calc_mean.h and initialize it in calc_mean.c here are this two files.

calc_mean.c

#include "calc_mean.h"

int mean(int a, int b)  
{  
return (a+b) / 2;  
}  

calc_mean.h

int mean(int, int);

Then I create make file and build archive (.a) file called Test_Archive.a using following make file

makefile

GCC := C:Tools
dk-toolchain
dk-standaloneinarm-linux-androideabi-gcc.exe
GPP := C:Tools
dk-toolchain
dk-standaloneinarm-linux-androideabi-g++.exe
AR  := C:Tools
dk-toolchain
dk-standaloneinarm-linux-androideabi-ar.exe

default: all

all: obj
    $(AR) r Test_Archive.a *.o

obj:
    $(GPP) -c *.c

I get Test_Archive.a archive file. Now I want to add this archive file to JNI and call mean function from my JNI project, for that I have created JNI and Java files which you can see below.


JAVA Layer

How you can see here in java layer I have TestJavaClass that have one native method called JMean this method have two int arguments and return also int.

public class TestJavaClass
{
    /** Default Constructor
     * 
     */
    public TestJavaClass( ) {

    }

    /** Test Function. 
     *  
     * @param a
     * @param b
     * @return
     */
    public native int JMean( int a, int b);
}

JNI Layer

This is JNI header file

Test_Library.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_Fido_OSTXLib_OSTX */

#ifndef _Included_com_Fido_OSTXLib_OSTX
#define _Included_com_Fido_OSTXLib_OSTX
#ifdef __cplusplus
extern "C" {
#endif

/*
 * Class:     com_Fido_OSTXLib_OSTX
 * Method:    JMean
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_Fido_OSTXLib_OSTX_JMean
  (JNIEnv *, jobject, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

Test_Library.c

#include "Test_Library.h"
#include "calc_mean.h"

JNIEXPORT jint JNICALL Java_com_Fido_OSTXLib_OSTX_JMean( JNIEnv* env, jobject thiz, jint a, jint b )
{
    return mean( a, b );
}

I put this two files Test_Library.h, Test_Library.c and calc_mean.h, Test_Archive.a and create make file Android.mk

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE           := Test_Library
LOCAL_SRC_FILES        := Test_Library.c

LOCAL_LDLIBS := -LTest_Archive.a

include $(BUILD_SHARED_LIBRARY)

but when I want to create .so file, the following error occurs, why ?

$ ../../ndk-build Compile thumb : Test_Library <= Test_Library.c SharedLibrary : libTest_Library.so C:/cygwin/home/android-ndk-r5b/Fido/ProjectOSTX/obj/local/armeabi/objs/OSTX_Library/OSTX_Library.o: In function Java_com_Fido_OSTXLib_OSTX_JMean': C:/cygwin/home/android-ndk-r5b/Fido/ProjectOSTX/jni/OSTX_Library.c:6: undefined reference tomean' collect2: ld returned 1 exit status make: * [/home/android-ndk-r5b/Fido/ProjectOSTX/obj/local/armeabi/libOSTX_Library.so] Error 1

Why undefined reference to `mean' ?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Here is what I did to build the code successfully:

  1. In your Eclipse Android Project create the folder jni.

  2. Add the files calc_mean.c; calc_mean.h; Test_Library.c and Android.mk to the jni folder

  3. Remove the line #include "Test_Library.h"from the Test_Library.c file. Everything else can stay as it is.

  4. In the cygwin command line go to the root folder of your Eclipse Android project and run ndk-build

If you want to build your calc_mean also as static library during the ndk-build process then you can change your Android.mk file as follows:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE           := Test_Archive
LOCAL_SRC_FILES        := calc_mean.c

# compiler flags.
LOCAL_LDLIBS   = -lz -lm
LOCAL_CFLAGS   = -Wall -pedantic -std=c99 -g

include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := Test_Library
LOCAL_STATIC_LIBRARIES := Test_Archive
LOCAL_SRC_FILES := Test_Library.c


include $(BUILD_SHARED_LIBRARY)

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

...