在 Windows 上使用 CMake 在 Android Studio 中构建+链接 libpng [英] Building+linking libpng in Android Studio with CMake on Windows

查看:31
本文介绍了在 Windows 上使用 CMake 在 Android Studio 中构建+链接 libpng的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Windows 10 上使用 Android Studio 和 CMake 启动本机应用程序项目,但我一直坚持包含 libpng.

I'm trying to start a native app project with Android Studio and CMake on Windows 10, but I'm stuck at including libpng.

对于初学者来说,这是我第一次看到 CMakeLists.txt 文件.我花了一天时间才弄清楚 target_link_libraries(native-activity ... png) 不可能是 target_link_libraries(png native-activity ...) 因为所有的错误消息是关于文件未创建和命令由于缺少工具链中的要求而失败(为什么基本错误位于列表末尾?不酷!).

For starters it's the first time I've seen a CMakeLists.txt file. It took me a day to figure out target_link_libraries(native-activity ... png) could not be target_link_libraries(png native-activity ...) since all the error messages were about files not being created and commands failing due to missing requirements from the toolchain (why were the essential errors at the end of the list? not cool!).

最终设法在项目中包含 libpng 后,我现在收到一个构建错误:

After finally managing to include libpng in the project I now get a build error:

Error:Execution failed for task ':app:externalNativeBuildDebug'.
...
error: unknown target CPU 'armv5te'
CMake Error at scripts/genout.cmake:78 (message):
    Failed to generate
    C:/APP_PATH/app/libpng-1.6.28/build/scripts/symbols.out.tf1
ninja: build stopped: subcommand failed.

我已经递归地搜索了我的项目、.android、.AndroidStudio2.2 目录以及文件名,除了 genout.cmake 之外,我完全没有发现 armv5te 的任何内容.我的 abiFilters 行是 abiFilters 'x86'.

I've recursively grep'ed my project, .android, .AndroidStudio2.2 directories, as well as filenames, and found absolutely nothing with armv5te except for the genout.cmake. My abiFilters line is abiFilters 'x86'.

如何构建 libpng 以链接到我的本机应用程序?此外,在 Android Studio 中,它显示该项目现在包含 libpng 源文件(至少有 9 个专用于它的项目!).有什么办法可以去掉吗?

How do I build libpng to link to my native app? Also, in Android Studio it shows the project now includes the libpng source files (with no less than 9 projects dedicated to it!). Is there any way to remove it?

这是我的 CMakeLists.txt:

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)

# build native_app_glue as a static lib
add_library(app-glue STATIC ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)

set(png_src_dir ../../../../libpng-1.6.28)
set(PNG_STATIC ON)
add_subdirectory(${png_src_dir} ${png_src_dir}/build)

# now build app's shared lib
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14")

add_library(native-activity SHARED
            main.cpp logger.cpp logger.h game.cpp game.h
            shaders.cpp shaders.h assets.cpp assets.h)

target_include_directories(native-activity PRIVATE ${ANDROID_NDK}/sources/android/native_app_glue
                           C:/devlibs/include
                           ${png_src_dir})

# add lib dependencies
target_link_libraries(native-activity app-glue android log EGL GLESv2 png)

推荐答案

我已经管理 libpng 以作为静态库与 android NDK 应用程序(CMake 构建系统而不是 Android.mk)一起使用.我使用了 libpng-android 重新打包.以下是我做过的事情:

I've managed libpng to work with android NDK app (CMake build system instead of Android.mk) as static library. I used libpng-android repackaging. Here are things I've done:

  1. git clone https://github.com/julienr/libpng-android.git${YOUR_LIBS_FOLDER} (我使用了 ${ANDROID_NDK_ROOT_DIRECTORY}/来源/android).
  2. ${ANDROID_NDK_ROOT_DIRECTORY}(对我来说是 home/username/Android/sdk/ndk-bundle)添加到全局 $PATH 变量构建脚本需要).
  3. 使用ndk-build 构建lib(在lib 目录中有./build.sh).库将针对不同的ABI 目标(arm64-v8a、<代码>armeabi、x86_64 等).
  4. 此时,您在 ${YOUR_LIBS_FOLDER}/libpng-android/jnilibpng.a 处有库头文件 ${YOUR_LIBS_FOLDER}/libpng-android/obj/local/${ANDROID_ABI}/,其中 ${ANDROID_ABI} 是目标平台.
  5. 最后,您可以在 CMakeLists.txt 中包含 lib.libpng 需要 zlib 压缩库,因此您还需要链接到它(zlib 由 android studio 提供,所以只需添加 -lz 标志).
  1. git clone https://github.com/julienr/libpng-android.git into ${YOUR_LIBS_FOLDER} (I used ${ANDROID_NDK_ROOT_DIRECTORY}/sources/android).
  2. Add ${ANDROID_NDK_ROOT_DIRECTORY} (home/username/Android/sdk/ndk-bundle for me) to global $PATH variable which is needed for build script).
  3. Build lib with ndk-build (there is ./build.sh for that in directory with lib). Library will be built for different ABI targets ( arm64-v8a, armeabi, x86_64 etc).
  4. At this point you have library headers at ${YOUR_LIBS_FOLDER}/libpng-android/jni and libpng.a at ${YOUR_LIBS_FOLDER}/libpng-android/obj/local/${ANDROID_ABI}/, where ${ANDROID_ABI} is target platform.
  5. Finally you could include lib at CMakeLists.txt. libpng requires zlib compression library so you need to link against it aswell (zlib is provided by android studio so just add -lz flag).

这是我的 CMakeLists.txt 中的相关部分:

Here is related piece from my CMakeLists.txt:

add_library(libpng STATIC IMPORTED)
set_target_properties(libpng PROPERTIES IMPORTED_LOCATION
    ${YOUR_LIBS_FOLDER}/libpng-android/obj/local/${ANDROID_ABI}/libpng.a)

add_library(appManager SHARED src/main/cpp/appManager.cpp)

target_include_directories(appManager PRIVATE ${YOUR_LIBS_FOLDER}/libpng-android/jni/)

target_link_libraries(appManager
                       android
                       libpng
                       z)

有几点需要注意:

  • ${ANDROID_ABI} 是 Android Studio 构建系统设置的变量.
  • 再一次:你需要链接到zlib,这就是我们有的原因libpng z 而不是 target_link_libraries 中的 libpng.
  • ${ANDROID_ABI} is a variable set by Android Studio build system.
  • Once again: you need to link against zlib, that is why we have libpng z instead of libpng in target_link_libraries.

这篇关于在 Windows 上使用 CMake 在 Android Studio 中构建+链接 libpng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆