使用ndk-build链接现有的静态库

时间:2013-09-19 18:22:23

标签: c++ c++11 android-ndk cmake static-linking

我有一个库,我使用cmake和android-cmake为Android编译并获得静态库。

然后我尝试使用这样的Android.mk文件将我的测试项目与这个静态库链接:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := ../test.cxx
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../src
LOCAL_CFLAGS := -std=gnu++11 -D__ANDROID__
LOCAL_CPP_FEATURES += exceptions
LOCAL_LDLIBS += -L.. -ljson++
include $(BUILD_EXECUTABLE)

和Application.mk

NDK_TOOLCHAIN_VERSION = 4.7
APP_STL := gnustl_static

json ++这里是我之前使用cmake构建的库的名称。

链接时失败
  

../ libjson ++。a(object.cpp.o):object.cpp:function json :: error_json_object_invalid_type(void const *,json :: object_type,json :: object_type,char const *):error:undefined reference到'std :: basic_ostringstream,std :: allocator> :: ~basic_ostringstream()'   ../libjson++.a(object.cpp.o):object.cpp:function json :: error_json_object_invalid_type(void const *,json :: object_type,json :: object_type,char const *):error:undefined reference to'VTT对于std :: basic_ostringstream,std :: allocator>'   ../libjson++.a(object.cpp.o):object.cpp:function json :: error_json_object_invalid_type(void const *,json :: object_type,json :: object_type,char const *):error:undefined reference to'vtable对于std :: basic_ostringstream,std :: allocator>'   ../libjson++.a(object.cpp.o):object.cpp:function json :: error_json_object_invalid_type(void const *,json :: object_type,json :: object_type,char const *):error:undefined reference to'vtable对于std :: basic_stringbuf,std :: allocator>'   ../libjson++.a(object.cpp.o):object.cpp:function json :: error_json_object_no_such_key(void const *,void const *,unsigned int):error:未定义引用'std :: basic_ostringstream,std :: allocator> :: ~basic_ostringstream()'   ../libjson++.a(object.cpp.o):object.cpp:function json :: error_json_object_no_such_key(void const *,void const *,unsigned int):error:undefined reference to'VTT for std :: basic_ostringstream,std :: allocator>'   ../libjson++.a(object.cpp.o):object.cpp:function json :: error_json_object_no_such_key(void const *,void const *,unsigned int):error:undefined reference to'vtable for std :: basic_ostringstream,std :: allocator>'   ../libjson++.a(object.cpp.o):object.cpp:function json :: error_json_object_no_such_key(void const *,void const *,unsigned int):error:undefined reference to'vtable for std :: basic_stringbuf,std :: allocator>'   ../libjson++.a(object.cpp.o):object.cpp:function std :: basic_stringbuf,std :: allocator> :: ~basic_stringbuf():error:未定义引用'vtable for std :: basic_stringbuf, std :: allocator>'   collect2:错误:ld返回1退出状态   make: * [obj / local / armeabi / test]错误1

此错误是由libgnustl_static.a在-ljson ++之前调用编译器引起的:

/opt/android-ndk/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ -Wl,--gc-sections -Wl,-z,nocopyreloc --sysroot=/opt/android-ndk/platforms/android-3/arch-arm ./obj/local/armeabi/objs/test/__/test.o /opt/android-ndk/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi/libgnustl_static.a -lgcc -no-canonical-prefixes  -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now  -L/opt/android-ndk/platforms/android-3/arch-arm/usr/lib -L.. -ljson++ -lc -lm -o ./obj/local/armeabi/test

所以可以通过添加

来解决
 -L${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/ -lgnustl_static

到LOCAL_LDLIBS

现在的问题是:如果它不属于我的项目并使用不同的构建系统编译,那么在现有静态库中链接的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

您可以在Android.mk文件中定义它,如下所示:

include $(CLEAR_VARS)

LOCAL_MODULE          := json++
LOCAL_MODULE_FILENAME := json++_static
LOCAL_SRC_FILES := path/to/libjson++.a

include $(PREBUILT_STATIC_LIBRARY)

然后添加此

LOCAL_WHOLE_STATIC_LIBRARIES := json++