如何使用Qt Creator创建一个不依赖于Android的单个本机共享库

时间:2016-12-01 17:58:00

标签: android qt android-ndk qmake static-linking

我使用Qt Creator创建了一个共享库,我添加了Android SDK,Android NDK和Android Qt工具包。然后我成功编译了我的Android库。我甚至成功地在Android应用程序中测试了它。

由于我没有使用Qt库,我的库不依赖于庞大的Qt库。但出乎意料的是,这是我的依赖:

[matin@Lenovo-X1-Fedora ~]$ ndk-depends libMatinChess.so 
WARNING: Could not find library: libgnustl_shared.so
libMatinChess.so
libz.so
libstdc++.so
libm.so
liblog.so
libgnustl_shared.so
libdl.so
libc.so

当我检查libgnustl_shared.so时,它的大小超过5 MB。因此,我必须在每个项目中将这个巨大的库放在我的小库旁边。

另一个选择是静态链接它。我之前曾问过the question如何静态链接依赖关系,我发现可以通过在我的.pro文件中添加QMAKE_LFLAGS += -static来实现:

此标志完美无缺,并消除了stdc ++对Windows编译的依赖性。但在android中我得到以下错误:

error: cannot find -lgnustl_shared
error: cannot find -llog
error: cannot find -lz
error: cannot find -ldl

我搜索了我的android-ndk文件夹,我意识到其中没有liblog.alibz.alibdl.a个文件但是有一个libgnustl_static.a文件。< / p>

我尝试使用LIBS += -Lpath/to/libdir -lgnustl_static添加它,但结果是一样的。

在上一个问题中提到的CMake中有一个解决方案,可以选择在makefile中设置APP_STL := gnustl_static。但在QMake中似乎没有相应的东西。

一个复杂的问题是,当我使用CONFIG += static时,它会成功编译,但我的库不再共享了。它变成了一个静态库。

如何静态链接gnustl以使我的库无需其他依赖项?

修改

我读了编译输出并找到了以下行:

  

/home/matin/Applications/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++   --sysroot = /家庭/晨报/应用/ Android的NDK-R13B /平台/ Android的9 /弓臂/   -static -Wl, - no-undefined -Wl,-z,noexecstack -shared -Wl,-soname,libMatinChess.so -o libMatinChess.so matinchessdll.o bishop.o piece.o board.o king.o memorymanager。 o pawn.o queen.o   blackpawn.o knight.o rook.o whitepawn.o squarelist.o game.o   boardhistory.o   -L /家庭/晨报/应用/ Android的NDK-R13B /来源/ CXX-STL / GNU-的libstdc ++ / 4.9 /库/ armeabi-V7A   -L /家庭/晨报/应用/ Android的NDK-R13B /平台/ Android的9 /弓臂// usr / lib目录   -lgnustl_shared -llog -lz -lm -ldl -lc -lgcc

我无法使用LIB -= -lgnustl_shared

删除gnustl_shared

2 个答案:

答案 0 :(得分:0)

通过读取编译输出,我手动执行了以下命令,并创建了1 MB大小的库。它工作正常。

  

/home/matin/Applications/android-ndk-r13b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++   --sysroot = /家庭/晨报/应用/ Android的NDK-R13B /平台/ Android的9 /弓臂/   -Wl, - no-undefined -Wl,-z,noexecstack -shared -Wl,-soname,libMatinChess.so -o libMatinChess.so matinchessdll.o bishop.o piece.o board.o king.o memorymanager.o pawn .o queen.o   blackpawn.o knight.o rook.o whitepawn.o squarelist.o game.o   boardhistory.o   -L /家庭/晨报/应用/ Android的NDK-R13B /来源/ CXX-STL / GNU-的libstdc ++ / 4.9 /库/ armeabi-V7A   -L /家庭/晨报/应用/ Android的NDK-R13B /平台/ Android的9 /弓臂// usr / lib目录   -lgnustl_static

但我仍然不知道如何在QMake中自动执行此命令

答案 1 :(得分:0)

在脚本yaml.indent()下面帮助了我,我在项目中包含了一些QtCreator相关错误:

android.pri

使用样本:

## this file changes many values set by "Qt/mkspec/android-g++/qmake.conf"
## since the failed to work with newest Android SDK and/or NDK
##
## use below lines before including this file
##
#ANDROID_API = 21
#ANDROID_ARCH = armeabi-v7a

#do you after using this have still problems?
#there is an bug inside "QtCreator" (not inside "Gradle")
#   that leads to .o and .so files get not found
#solved: not required to change "QMAKE_LIBDIR" just open "AndroidManifest.xml"
#   and change "Minimum required SDK" to "not set" save then restore to last
#   value and save agian to force "QtCreator" update

isEmpty(ANDROID_API): ANDROID_API = 21
isEmpty(ANDROID_ARCH): ANDROID_ARCH = armeabi-v7a #ANDROID_TARGET_ARCH=armeabi-v7a

#remove old values
CONFIG         -= $$ANDROID_PLATFORM
QMAKE_CFLAGS   -= --sysroot=$$ANDROID_PLATFORM_ROOT_PATH
QMAKE_CXXFLAGS -= --sysroot=$$ANDROID_PLATFORM_ROOT_PATH
QMAKE_LFLAGS   -= --sysroot=$$ANDROID_PLATFORM_ROOT_PATH

#NDK Root directory
ANDROID_NDK_ROOT = $$(ANDROID_NDK_ROOT) #first try Environment variable
isEmpty(ANDROID_NDK_ROOT) | !exists($$ANDROID_NDK_ROOT) {
    ANDROID_NDK_ROOT = D:/android/sdk/ndk-bundle }
NDK_ROOT = $$ANDROID_NDK_ROOT

#API Level
ANDROID_NDK_PLATFORM = android-$$ANDROID_API
ANDROID_PLATFORM = $$ANDROID_NDK_PLATFORM
CONFIG  += $$ANDROID_PLATFORM
DEFINES += __ANDROID_API__=$$ANDROID_API

#Architecture
ANDROID_TARGET_ARCH = $$ANDROID_ARCH
equals(ANDROID_TARGET_ARCH, x86): ANDROID_ARCHITECTURE = x86
else: equals(ANDROID_TARGET_ARCH, x86_64): ANDROID_ARCHITECTURE = x86_64
else: equals(ANDROID_TARGET_ARCH, mips): ANDROID_ARCHITECTURE = mips
else: equals(ANDROID_TARGET_ARCH, mips64): ANDROID_ARCHITECTURE = mips64
else: equals(ANDROID_TARGET_ARCH, arm64-v8a): ANDROID_ARCHITECTURE = arm64
else: ANDROID_ARCHITECTURE = arm

#API Path
ANDROID_PLATFORM_ROOT_PATH  = $$NDK_ROOT/platforms/$$ANDROID_PLATFORM/arch-$$ANDROID_ARCHITECTURE/
ANDROID_PLATFORM_PATH  = $$ANDROID_PLATFORM_ROOT_PATH/usr
QMAKE_CFLAGS   += --sysroot=$$ANDROID_PLATFORM_ROOT_PATH
QMAKE_CXXFLAGS += --sysroot=$$ANDROID_PLATFORM_ROOT_PATH
QMAKE_LFLAGS   += --sysroot=$$ANDROID_PLATFORM_ROOT_PATH

# used to compile platform plugins for android-4 and android-5
QMAKE_ANDROID_PLATFORM_INCDIR = $$NDK_ROOT/sysroot/usr/include #headers bundled
QMAKE_ANDROID_PLATFORM_LIBDIR = $$ANDROID_PLATFORM_PATH/lib #same as before

ANDROID_SOURCES_CXX_STL_LIBDIR = $$NDK_ROOT/sources/cxx-stl/gnu-libstdc++/$$NDK_TOOLCHAIN_VERSION/libs/$$ANDROID_TARGET_ARCH
ANDROID_SOURCES_CXX_STL_INCDIR = $$NDK_ROOT/sources/cxx-stl/gnu-libstdc++/$$NDK_TOOLCHAIN_VERSION/include $$ANDROID_SOURCES_CXX_STL_LIBDIR/include

equals(ANDROID_TARGET_ARCH, x86_64)|equals(ANDROID_TARGET_ARCH, mips64): \
    QMAKE_ANDROID_PLATFORM_LIBDIR = $${QMAKE_ANDROID_PLATFORM_LIBDIR}64

#additionl fix

QMAKE_CFLAGS += -Wno-attributes #ignore Android Macros
QMAKE_CFLAGS += \
    -Wno-unused-parameter \
    -Wno-unused-variable \
    -Wno-unused-but-set-variable \
    -Wno-unused-value \
    -Wno-unused-function

INCLUDEPATH += $$ANDROID_NDK_ROOT/sysroot/usr/include
INCLUDEPATH += $$ANDROID_NDK_ROOT/sysroot/usr/include/arm-linux-androideabi

## you most times need set "ANDROID_PACKAGE_SOURCE_DIR" manualys
isEmpty(ANDROID_PACKAGE_SOURCE_DIR) {
    DISTFILES += \
        $$PWD/res/android/AndroidManifest.xml \
        $$PWD/res/android/gradle/wrapper/gradle-wrapper.jar \
        $$PWD/res/android/gradlew \
        $$PWD/res/android/res/values/libs.xml \
        $$PWD/res/android/build.gradle \
        $$PWD/res/android/gradle/wrapper/gradle-wrapper.properties \
        $$PWD/res/android/gradlew.bat

    ANDROID_PACKAGE_SOURCE_DIR = $$PWD/res/android

    !build_pass:warning(no ANDROID_PACKAGE_SOURCE_DIR defaulted to $$ANDROID_PACKAGE_SOURCE_DIR)
}
#ANDROID_EXTRA_LIBS = $$PWD/libTest.so ## you may need
#ANDROID_DEPLOYMENT_SETTINGS_FILE = $$PWD/android-settings.json ## no need ever

#QML_IMPORT_PATH
#QML_DESIGNER_IMPORT_PATH
#QMAKE_PROJECT_NAME

#isEmpty(ANDROID_PACKAGE_SOURCE_DIR) {
#    # note that $$PWD/android/assets directory is "QFile::ReadOnly" on android
#    android: varDirInstall.path =/assets
#    else: varDirInstall.path =$$OUT_PWD
#
#    varDirInstall.files = $$files($$PWD/android/assets)
#    win32: varDirInstall.files ~= s|\\\\|/|g
#    varDirInstall.depends += FORCE
#    INSTALLS += varDirInstall
#}

但我确实在ANDROID_API = 21 ANDROID_ARCH = armeabi-v7a include($$PWD/android.pri)

的任何方式都会遇到以下错误
  

.pro文件没有设置Android arch。构建/部署时出错   project vpnAndroid(kit:Qt5_android_armeabi-v7a)执行步骤时   &#34;部署到Android设备&#34;

这个特殊的QtCreator相关错误需要花费一些时间来修复:

  1. 关闭IDE并删除所有QtCreator设置文件(在Windows TEMPLATE = app中)
  2. 再次启动IDE并按此顺序重新配置Android路径,编译器,转发器和套件
  3. 关闭IDE以保存更改
  4. 备份前面提到的文件夹以节省时间,见下文:
  5. 与Android相关的QtCreator插件会在设置中存储失败,因此您需要备份,因为插件可能会再次执行此操作