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

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

问题描述

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



Cmake成功生成我的项目,但是当我尝试进入generate目录并尝试运行make得到以下错误:

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



<我的项目使用C ++ 11,这里是我的 g ++ --version 结果:

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

感谢!

解决方案

Cmake和创建APK,你应该做:





 

    cmake_minimum_required(VERSION 2.8.3)

    project(testBuilder)


    include(Apk.cmakeREQUIRED)

    include_directories($ {ANDROID_NDK } / sources>

    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, - 根据需要, - gc-sections -Wl, - no-undefined -Wl, - strip-all -Wl,-rpath-link = $ {ANDROID_NDK_SYSROOT} / usr / lib / -L $ {ANDROID_NDK_SYSROOT} / usr / )

    add_library(test SHARED $ {TEST_SRC})

    target_link_libraries / li>
    set_target_properties(test 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> 
#include< android / log.h>

#define APPNAMETestApp

void android_main(struct android_app * state)
{
app_dummy //确保胶水没有被剥离

__android_log_print(ANDROID_LOG_INFO,APPNAME,HolyShit you did it!

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天全站免登陆