【问题标题】:Use ndk-build to link with an existing static library使用 ndk-build 链接现有的静态库
【发布时间】:2013-09-19 18:22:23
【问题描述】:

我有一个库,我使用 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: undefined reference to 'std::basic_ostringstream, std::分配器 >::~basic_ostringstream()' ../libjson++.a(object.cpp.o):object.cpp:function json::error_json_object_no_such_key(void const*, void const*, unsigned int): 错误: 未定义引用'VTT 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_ostringstream, std ::分配器>' ../libjson++.a(object.cpp.o):object.cpp:function json::error_json_object_no_such_key(void const*, void const*, unsigned int): 错误: 未定义引用'vtable for std::basic_stringbuf, std ::分配器>' ../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 ::分配器>' 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

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

【问题讨论】:

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


    【解决方案1】:

    您可以像这样在 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++
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多