gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:14:29 31 4
gpt4 key购买 nike

我有一个使用 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::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: undefined reference to 'vtable for std::basic_stringbuf, std::allocator >' collect2: error: ld returned 1 exit status make: * [obj/local/armeabi/test] Error 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

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

最佳答案

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

关于c++ - 使用 ndk-build 链接现有的静态库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18902217/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com