說明
系統(tǒng)環(huán)境:mac os
運(yùn)行條件: 安裝cmake 和 下載ndk
創(chuàng)建文件
源文件 hello.cpp
#include<stdio.h>
int main(){
printf("hello world \n");
return 0;
}
CMakeCache.txt 文件
cmake_minimum_required(VERSION 3.7)
#include(/Users/Shared/ShareLib/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake)
# 打印當(dāng)前的環(huán)境
if(ANDROID)
message(STATUS "ANDROID")
elseif(APPLE)
message(STATUS "APPLE")
elseif(WIN32)
message(STATUS "WIN32")
elseif(UNIX)
message(STATUS "UNIX")
else()
message(FATAL_ERROR "OTHER")
endif()
add_executable(hello hello.cpp)
target_include_directories(hello PRIVATE .)
# cmake -DCMAKE_TOOLCHAIN_FILE=/Users/Shared/ShareLib/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake ..
編譯工程
創(chuàng)建build目錄并進(jìn)入
在build目錄下執(zhí)行cmake
cmake -DCMAKE_TOOLCHAIN_FILE=/Users/Shared/ShareLib/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake ..
其中"/Users/Shared/ShareLib/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake"是本機(jī)機(jī)器ndk安裝交互編譯的cmake配置
輸出
-- ANDROID_PLATFORM not set. Defaulting to minimum supported version
16.
-- Check for working C compiler: /Users/Shared/ShareLib/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang
-- Check for working C compiler: /Users/Shared/ShareLib/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Users/Shared/ShareLib/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++
-- Check for working CXX compiler: /Users/Shared/ShareLib/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- ANDROID
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/wangshengxing/project/c/ndkcmake/hello/build
此時(shí)執(zhí)行 make 掘剪,輸出
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/hello.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello
以上hello即為可在安卓上執(zhí)行的文件
執(zhí)行文件
將hello文件push到手機(jī)的/data/local/tmp/目錄下
運(yùn)行文件
adb shell
cd /data/local/tmp/
./hello
則得到以下輸出