使用 Cmake 构建 Android NDK 项目 [英] Build Android NDK project with Cmake

查看:38
本文介绍了使用 Cmake 构建 Android NDK 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 android NDK 和 Cmake 生成我的 android 原生应用程序,所以我下载了 android-cmake 工具链.

Cmake 成功生成我的项目,但是当我尝试进入生成目录并尝试运行make"时,出现以下错误:

-- 配置完成-- 生成完成-- 构建文件已写入:/Users/ldz/Desktop/myProject[ 1%] 构建 CXX 对象 Project/src/Main/Core/CMakeFiles/Core.dir/Main/Main.cpp.oarm-linux-androideabi-g++:错误:无法识别的命令行选项-stdlib=libc++"

不知道这里出了什么问题,我的项目用的是C++11,这是我的g++ --version结果:

配置为:--prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1Apple LLVM 5.0 版(clang-500.2.76)(基于 LLVM 3.3svn)目标:x86_64-apple-darwin12.5.0线程模型:posix

谢谢!

解决方案

要使用 Cmake 构建 Android NDK 项目并创建 APK,您应该这样做:

  • 您应该使用 来自 taka-no-me 的分支,而不是使用 android-cmake.
  • 然后使用 Apk.cmake from pixellight.还从这个 repo 复制 [AndroidManifest.xml.in, LoadLibraries.java.in, strings.xml.in].
  • 有一个像这样的 CMakeLists.txt:
<上一页>

    cmake_minimum_required(版本 2.8.3)项目(testBuilder)包括(Apk.cmake"需要)include_directories(${ANDROID_NDK}/sources/android/native_app_glue)设置(TEST_SRC${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.csrc/Main.cpp)设置(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -ffor-scope -fno-rtti -fno-exceptions -pipe -ffunction-sections -fdata-sections -ffast-math -Wnon-virtual-dtor -Wreorder -wsign-promo -fvisibility=hidden -fvisibility-inlines-hidden -Wstrict-null-sentinel -Os -funroll-all-loops -fpeel-loops -ftree-vectorize")set(LINKER_FLAGS "${LINKER_FLAGS} -Wl,--根据需要 -Wl,--gc-sections -Wl,--no-undefined -Wl,--strip-all -Wl,-rpath-link=${ANDROID_NDK_SYSROOT}/usr/lib/-L${ANDROID_NDK_SYSROOT}/usr/lib/")add_library(test SHARED ${TEST_SRC})target_link_libraries(测试日志android)set_target_properties(test PROPERTIES COMPILE_DEFINITIONS "ANDROID")设置(APP_SHARED_LIBRARIES ${LIBRARY_OUTPUT_PATH}/libtest.so)android_create_apk(test "${CMAKE_BINARY_DIR}/apk" "${APP_SHARED_LIBRARIES}" "" "数据")

这是 Main.cpp

#include <android_native_app_glue.h>#include <android/log.h>#define APPNAME "TestApp"无效 android_main(结构 android_app* 状态){app_dummy();//确保胶水没有被剥离__android_log_print(ANDROID_LOG_INFO, APPNAME, "天哪,你做到了!");ANativeActivity_finish(state->activity);}

I would like to generate my android native application with the android NDK and Cmake, so, I've downloaded the android-cmake toolchain.

Cmake generate my project successfully, but when I try to go in the generate directory and try to run "make", I've got the following error:

-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ldz/Desktop/myProject
[  1%] Building CXX object Project/src/Main/Core/CMakeFiles/Core.dir/Main/Main.cpp.o
arm-linux-androideabi-g++: error: unrecognized command line option '-stdlib=libc++'

I don't know what is wrong here, my project use C++11, here is my g++ --version result:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix

Thanks!

解决方案

To build an Android NDK project with Cmake and create APK, you should do :

  • Instead of using android-cmake, you should use the fork from taka-no-me.
  • Then use Apk.cmake from pixellight. Copy also [AndroidManifest.xml.in, LoadLibraries.java.in, strings.xml.in] from this repo.
  • Have a CMakeLists.txt like this :

    cmake_minimum_required(VERSION 2.8.3) project(testBuilder) include("Apk.cmake" REQUIRED) include_directories(${ANDROID_NDK}/sources/android/native_app_glue) set(TEST_SRC ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c src/Main.cpp ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -ffor-scope -fno-rtti -fno-exceptions -pipe -ffunction-sections -fdata-sections -ffast-math -Wnon-virtual-dtor -Wreorder -Wsign-promo -fvisibility=hidden -fvisibility-inlines-hidden -Wstrict-null-sentinel -Os -funroll-all-loops -fpeel-loops -ftree-vectorize") set(LINKER_FLAGS "${LINKER_FLAGS} -Wl,--as-needed -Wl,--gc-sections -Wl,--no-undefined -Wl,--strip-all -Wl,-rpath-link=${ANDROID_NDK_SYSROOT}/usr/lib/ -L${ANDROID_NDK_SYSROOT}/usr/lib/") add_library(test SHARED ${TEST_SRC}) target_link_libraries(test log android) set_target_properties(test PROPERTIES COMPILE_DEFINITIONS "ANDROID") set(APP_SHARED_LIBRARIES ${LIBRARY_OUTPUT_PATH}/libtest.so) android_create_apk(test "${CMAKE_BINARY_DIR}/apk" "${APP_SHARED_LIBRARIES}" "" "Data")

This is Main.cpp

#include <android_native_app_glue.h>
#include <android/log.h>

#define APPNAME "TestApp"

void android_main(struct android_app* state)
{
    app_dummy(); // Make sure glue isn't stripped

    __android_log_print(ANDROID_LOG_INFO, APPNAME, "HolyShit you did it !");

    ANativeActivity_finish(state->activity);
}

这篇关于使用 Cmake 构建 Android NDK 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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