建立与CMake的Andr​​oid的NDK项目 [英] Build Android NDK project with Cmake

查看:645
本文介绍了建立与CMake的Andr​​oid的NDK项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成与Android NDK和我的CMake Android原生的应用程序,所以,我已经下载了的 Android的CMake的工具链。

CMake的成功生成我的项目,但是当我尝试去在生成目录,并尝试运行make,我有以下错误:

   - 配置DONE
- 生成完成
- 建立文件已被写入:/用户/ LDZ /桌面/ myProject的
[1%]大厦CXX对象项目/ src目录/主/核心/ CMakeFiles / Core.dir /主/ Main.cpp.o
臂Linux的androideabi-G ++:错误:无法识别的命令行选项'-stdlib =的libc ++

我不知道什么是错在这里,我的项目中使用C ++ 11,这里是我的 G ++ --version 结果:

 配置有: -  preFIX = /应用/ X code.app /内容/开发/ USR --with-GXX-包括-DIR = / USR /include/c++/4.2.1
(基于LLVM 3.3svn)苹果LLVM 5.0版(铛-500.2.76)
目标:x86_64的 - 苹果darwin12.5.0
线程模型:POSIX

谢谢!


解决方案

要建立与CMake的一个Android NDK项目并创建APK,你应该做的:


    cmake_minimum_required(版本2.8.3)
     
    项目(testBuilder)
     
    包括(Apk.cmake要求)
     
    include_directories($ {} ANDROID_NDK /来源/安卓/ native_app_glue)
     
    集(TEST_SRC
        $ {} ANDROID_NDK /sources/android/native_app_glue/android_native_app_glue.c
        SRC / Main.cpp的

     
    集(CMAKE_CXX_FLAGS$ {} CMAKE_CXX_FLAGS -std =的C ++ 0x -ffor范围的-fno-RTTI -fno-例外哌-ffunction截面-fdata区段-ffast-数学-Wnon虚析构函数-Wreorder - Wsign宣传-fvisibility =隐藏-fvisibility - 内联隐藏-Wstrict空前哨-Os -funroll-全循环-fpeel-循环-ftree-矢量)
    集(LINKER_FLAGS$ {} LINKER_FLAGS轮候册, - 按需轮候册, - GC-部分轮候册, - 无未定义轮候册, - 带​​状所有-Wl,-rpath链接= $ { ANDROID_NDK_SYSROOT} / usr / lib目录/ -L $ {} ANDROID_NDK_SYSROOT / usr / lib目录/)
     
    add_library(测试SHARED $ {} TEST_SRC)
     
    target_link_libraries(测试日志机器人)
    set_target_properties(测试属性COMPILE_DEFINITIONSAndroid的)
     
    集(APP_SHARED_LIBRARIES $ {} LIBRARY_OUTPUT_PATH /libtest.so)
     
    android_create_apk(测试$ {} CMAKE_BINARY_DIR / APK$ {} APP_SHARED_LIBRARIES数据)

这是Main.cpp的

 的#include< android_native_app_glue.h>
#包括LT&;机器人/ log.h>#定义APPNAMETestApp无效android_main(结构android_app *状态)
{
    app_dummy(); //确保胶水,不会被剥离    __android_log_print(ANDROID_LOG_INFO,APPNAMEHolyShit你做到了!);    ANativeActivity_finish(语句>活动);
}

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的Andr​​oid的NDK项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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