1

我有一个库,我使用 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 to '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 for 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 for 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 for 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: undefined reference to '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::分配器 >' ../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: undefined reference to 'vtable for std::basic_stringbuf, std::allocator >' collect2 : 错误: ld 返回 1 退出状态 make:collect2:错误:ld 返回 1 退出状态 make: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

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

4

1 回答 1

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++
于 2013-12-19T15:59:29.140 回答