使用Cmake构建Android NDK项目

时间:2013-10-06 10:18:23

标签: gcc android-ndk makefile cmake

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

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

-- 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++'

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

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

谢谢!

2 个答案:

答案 0 :(得分:17)

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

  • 您应该使用fork from taka-no-me
  • ,而不是使用android-cmake
  • 然后使用Apk.cmake from pixellight。从这个仓库复制[AndroidManifest.xml.in,LoadLibraries.java.in,strings.xml.in]。
  • 像这样拥有一个CMakeLists.txt:
    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")

这是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);
}

答案 1 :(得分:5)

基于Vi.:s回答我在github上做了一个android-cmake的克隆,并添加了一个名为android.apk.cmake的修改过的Apk.cmake。我使用NativeActivity而不是pixellight:s LoadLibraries.java。

克隆在这里: https://github.com/Discordia/android-cmake

我在Vi中创建了这个例子:答案: https://github.com/Discordia/android-cmake-example