我开始创建库作为共享库,但我认为创建一个共享库和其他静态库会更有效。当它全部共享时,它编译和链接很好,但转移到静态,我得到链接“未定义的引用”。
修改:我在一个Android.mk
Android.mk:
MY_LOCAL_PATH := $(call my-dir)
MY_LOCAL_CFLAGS := -DDEBUG
TARGET_PLATFORM := 'android-4'
LOCAL_PATH := $(MY_LOCAL_PATH)/../../Base
include $(CLEAR_VARS)
LOCAL_MODULE := Base
LOCAL_SRC_FILES := <Base src files>
include $(BUILD_STATIC_LIBRARY)
MY_LOCAL_STATIC_LIBRARIES := Base
MY_LOCAL_C_INCLUDES := $(MY_LOCAL_PATH)/../../Base
LOCAL_PATH := $(MY_LOCAL_PATH)/../../Framework
include $(CLEAR_VARS)
LOCAL_MODULE := Framework
LOCAL_C_INCLUDES := $(MY_LOCAL_C_INCLUDES)
LOCAL_SRC_FILES := <Framework src files>
LOCAL_CFLAGS := $(MY_LOCAL_CFLAGS)
include $(BUILD_STATIC_LIBRARY)
MY_LOCAL_STATIC_LIBRARIES += Framework
MY_LOCAL_C_INCLUDES += $(MY_LOCAL_PATH)/../../Framework
LOCAL_PATH := $(MY_LOCAL_PATH)/Graphics
include $(CLEAR_VARS)
LOCAL_MODULE := Graphics
LOCAL_SRC_FILES := <Graphics src files>
LOCAL_EXPORT_LDLIBS := -lGLESv1_CM
LOCAL_CFLAGS := $(MY_LOCAL_CFLAGS)
LOCAL_C_INCLUDES := $(MY_LOCAL_C_INCLUDES)
include $(BUILD_STATIC_LIBRARY)
MY_LOCAL_STATIC_LIBRARIES += Graphics
MY_LOCAL_C_INCLUDES += $(MY_LOCAL_PATH)/Graphics
LOCAL_PATH := $(MY_LOCAL_PATH)/Platform
include $(CLEAR_VARS)
LOCAL_MODULE := Platform
LOCAL_SRC_FILES := <Platform src files>
LOCAL_CFLAGS := $(MY_LOCAL_CFLAGS)
LOCAL_C_INCLUDES := $(MY_LOCAL_C_INCLUDES)
include $(BUILD_STATIC_LIBRARY)
MY_LOCAL_STATIC_LIBRARIES += Platform
MY_LOCAL_C_INCLUDES += $(MY_LOCAL_PATH)/Platform
LOCAL_PATH := $(MY_LOCAL_PATH)
include $(CLEAR_VARS)
LOCAL_MODULE := Final
LOCAL_SRC_FILES := <Final src files>
LOCAL_STATIC_LIBRARIES := $(MY_LOCAL_STATIC_LIBRARIES)
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := $(MY_LOCAL_CFLAGS)
LOCAL_C_INCLUDES := $(MY_LOCAL_C_INCLUDES)
include $(BUILD_SHARED_LIBRARY)
ndk-build V=1 -B
的最后一行:
SharedLibrary : libFinal.so
/Users/robbie/Library/Frameworks/Android-NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86/bin/arm-linux-androideabi-g++ -Wl,-soname,libFinal.so -shared --sysroot=/Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm <object files> /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libBase.a /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libFramework.a /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libGraphics.a /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libPlatform.a /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libstdc++.a /Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm/usr/lib/libc.so /Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm/usr/lib/libstdc++.so /Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm/usr/lib/libm.so -Wl,--no-undefined -Wl,-z,noexecstack -L/Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm/usr/lib -llog -lGLESv1_CM -lstdc++ -Wl,-rpath-link=/Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm/usr/lib -lsupc++ -o /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libFinal.so
/Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libPlatform.a(ATexture.o): In function `ATexture':
/Users/robbie/Documents/Apps/Revolution/Android/jni/SpinTap/ATexture.cpp:9: undefined reference to `TextureRenderer::TextureRenderer(unsigned int)'
/Users/robbie/Documents/Apps/Revolution/Android/jni/SpinTap/ATexture.cpp:9: undefined reference to `TextureRenderer::TextureRenderer(unsigned int)'
Edit2: TextureRenderer位于Graphics中,包含在内。
有没有人知道为什么它可能无法正常工作以及如何解决它?
答案 0 :(得分:12)
这看起来像是一个链接顺序问题。
您的命令行是:
arm-linux-androideabi-g++ -Wl,-soname,libFinal.so -shared \
libBase.a libFramework.a libGraphics.a libPlatform.a -o libFinal.so
,错误是
libPlatform.a(ATexture.o): In function `ATexture':
ATexture.cpp:9: undefined reference to `TextureRenderer'
ATexture.cpp:9: undefined reference to `TextureRenderer'
TextureRenderer在Graphics中。但是libGraphics在命令行上的libPlatform之前。 g ++将按照给定的顺序在命令行上搜索每个库,加载函数以解析外部引用。它将读取一次libGraphics,加载解析外部引用的函数并转到libPlatform。
尝试将LOCAL_STATIC_LIBRARIES := $(MY_LOCAL_STATIC_LIBRARIES)
更改为LOCAL_STATIC_LIBRARIES := Platform Graphics Framework Base
,看看你是如何继续的。
答案 1 :(得分:2)
在Android.mk
中,确保使用正确的调用引用静态库:
LOCAL_STATIC_LIBRARIES := mystaticlibproj
在调用包含$(BUILD_SHARED_LIBRARY)
之前。
然后,在文件末尾,调用以导入静态lib模块
$(call import-module, mystaticlibproj)
如果您仍遇到问题,请发布详细构建日志(ndk-build V=1 -B
)和Android.mk