15

我想建立一个共享库。要构建它,我需要调用另一个共享库。这是我所做的:

1.创建一个Android项目,命名为“BuildLib”,在项目目录下新建文件夹“jni”。jni文件夹内容:

jni-->Android.mk
-->Application.mk
-->add.cpp
-->add.h add.cpp 只是做两个数字相加:

添加.h:

int add(int a,int b);

添加.cpp:

#include "add.h"  
int add(int a,int b){
    return a+b;}

安卓.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES  := add.cpp 
LOCAL_MODULE     := add
include $(BUILD_SHARED_LIBRARY)

构建项目后,我在目录下获得了 libadd.so $(BUILDLIB)/libs/armeabi/

创建另一个名为“CallLib”的 Android 项目。将libadd.so和复制add.h到 jni 文件夹,创建Android.mkApplication.mkcall_add.cpp.

安卓.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := libadd.so
LOCAL_MODULE := add_prebuilt
include $(PREBUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_SRC_FILES  := call_add.cpp 
LOCAL_MODULE     :=  native
LOCAL_SHARED_LIBRARIES := add_prebuilt
include $(BUILD_SHARED_LIBRARY)

call_add.cpp:

#include "add.h"
int call_add(){return add(1,2);}

毕竟,我构建了 CallLib 项目,但得到了错误:

未定义对'add(int, int)'的引用;

我认为libadd.so找不到,但我不知道如何修改。有谁知道我该如何解决这个问题?任何帮助将不胜感激。

4

2 回答 2

4

在您的第二个Android.mk中,尝试将第一个模块替换为:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := libadd.so
LOCAL_MODULE := add_prebuilt
LOCAL_EXPORT_C_INCLUDES := add.h
include $(PREBUILD_SHARED_LIBRARY)

LOCAL_EXPORT_C_INCLUDES标志应将标头信息附加到add_prebuilt模块,以便它可以与您的最终库链接。

于 2013-07-18T08:23:05.767 回答
1

以防万一有人需要它:

保持链接器快乐的一种有点骇人听闻的方法:

LOCAL_LDLIBS := -llog

或者

LOCAL_LDLIBS := -L$(LOCAL_PATH)/lib -lMyStuff

不那么骇人听闻:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := xyz
LOCAL_SRC_FILES += xyz/xyz.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)    # this builds libxyz.so


include $(CLEAR_VARS)
LOCAL_MODULE    := abc
LOCAL_SHARED_LIBRARIES := xyz    # <=== !!! this makes libabc.so dependent on libxyz.so
LOCAL_SRC_FILES := abc/abc.c
#LOCAL_LDLIBS := ...
include $(BUILD_SHARED_LIBRARY)    # this builds libabc.so
于 2014-06-10T10:14:31.097 回答