无法创建内部带有静态的共享库 [英] Can't create shared library with static inside

查看:16
本文介绍了无法创建内部带有静态的共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我搜索整个互联网,但我找不到答案......

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

我创建了一个简单的函数 int mean(int, int); 并将它放在 calc_mean.h 中并在 calc_mean.c 中初始化它> 这是这两个文件.

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.

#include "calc_mean.h"

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

calc_mean.h

int mean(int, int);

然后我使用以下生成文件创建生成文件并生成名为 Test_Archive.a 的归档 (.a) 文件

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

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

我得到 Test_Archive.a 存档文件.现在我想将此存档文件添加到 JNI 并从我的 JNI 项目中调用 mean 函数,为此我创建了 JNI 和 Java 文件,您可以在下面看到.

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 层看到这里我有 TestJavaClass,它有一个名为 JMean 的本机方法,这个方法有两个 int 参数并且还返回 int.

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 层

这是JNI头文件


JNI Layer

This is JNI header file

/* 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 );
}

我把这两个文件Test_Library.hTest_Library.ccalc_mean.hTest_Archive.a> 并创建 make 文件 Android.mk

I put this two files Test_Library.h, Test_Library.c and calc_mean.h, Test_Archive.a and create make file 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)

但是当我想创建.so文件时,出现以下错误,为什么?

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

$ ../../ndk-build 编译拇指:Test_Library <= Test_Library.c共享库:libTest_Library.soC:/cygwin/home/android-ndk-r5b/Fido/ProjectOSTX/obj/local/armeabi/objs/OSTX_Library/OSTX_Library.o:在函数 Java_com_Fido_OSTXLib_OSTX_JMean' 中:C:/cygwin/home/android-ndk-r5b/Fido/ProjectOSTX/jni/OSTX_Library.c:6:对mean' collect2 的未定义引用:ld 返回 1 个退出状态 make:*[/home/android-ndk-r5b/Fido/ProjectOSTX/obj/local/armeabi/libOSTX_Library.so]错误 1

$ ../../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

为什么未定义对`mean'的引用?

推荐答案

因为我认为主要原因是编译器设置,所以我在 makefile 和我的 .链接到共享库的存档,这里是修改.

As I suppos the main reason was compiler settings, I make some modifications in makefile and my .a archive linked to shared library, here are modifications.

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

OPTIONS  :=
-fpic 
-ffunction-sections 
-funwind-tables  
-fstack-protector 
-D__ARM_ARCH_5__ 
-D__ARM_ARCH_5T__ 
-D__ARM_ARCH_5E__ 
-D__ARM_ARCH_5TE__ 
-Wno-psabi 
-march=armv5te 
-mtune=xscale 
-msoft-float 
-mthumb 
-Os 
-fomit-frame-pointer 
-fno-strict-aliasing 
-finline-limit=64 
-DANDROID 
-Wa, 
-O2 
-DNDEBUG 
-g 

default: all


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

obj:
    $(GCC) $(OPTIONS) -c *.c

这篇关于无法创建内部带有静态的共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆