快速编译安卓cpp代码
快速编译安卓cpp代码

快速编译安卓cpp代码

Tags
Published
Sub-item
Parent item
Author
AI summary
  1. 编译脚本的方式 只需要配置一下环境变量 NDK_HOME 即可 ( cmake_build.ps1 可以直接放进NDK_build的同级文件,方便我们直接命令行调用 )
    1. # Path to ndk $NDK = $env:NDK_HOME # Path to cmake $CMAKE = "cmake" $BUILD_PATH = "cmake_builds" # Targets $ABIs = "arm64-v8a", "armeabi-v7a", "x86", "x86_64" foreach ($abi in $ABIs) { Write-Host "==========================" Write-Host "= Building $abi" Write-Host "==========================" & $CMAKE -S . -B "$BUILD_PATH/$abi" -G "Ninja" ` -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE ` -DCMAKE_BUILD_TYPE=Release ` -DCMAKE_TOOLCHAIN_FILE="$NDK/build/cmake/android.toolchain.cmake" ` -DANDROID_NDK="$NDK" ` -DANDROID_ABI="$abi" ` -DANDROID_NATIVE_API_LEVEL=21 & $CMAKE --build "$BUILD_PATH/$abi" } Pause
  1. 使用vscode帮我们预处理cmake工程 ( 放在和cmake同级目录即可 )
    1. { "version": 4, "cmakeMinimumRequired": { "major": 3, "minor": 19, "patch": 0 }, "configurePresets": [ { "name": "Android_ARM64", "hidden": false, "generator": "Ninja", "binaryDir": "${sourceDir}/build/arm64-v8a", "cacheVariables": { "ANDROID_NATIVE_API_LEVEL": "24", "ANDROID_ABI": "arm64-v8a", "ANDROID_PLATFORM": "android-24", "ANDROID_NDK": "${env.NDK_HOME}", "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", "CMAKE_SYSTEM_VERSION": "24", "CMAKE_SYSTEM_NAME": "Android", "CMAKE_TOOLCHAIN_FILE": "${env.NDK_HOME}/build/cmake/android.toolchain.cmake", "CMAKE_BUILD_TYPE": "Release" } }, { "name": "Android_ARM32", "hidden": false, "generator": "Ninja", "binaryDir": "${sourceDir}/build/armeabi-v7a", "cacheVariables": { "ANDROID_NATIVE_API_LEVEL": "24", "ANDROID_ABI": "armeabi-v7a", "ANDROID_PLATFORM": "android-24", "ANDROID_ARM_MODE": "arm", "ANDROID_NDK": "${env.NDK_HOME}", "CMAKE_SYSTEM_VERSION": "24", "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", "CMAKE_TOOLCHAIN_FILE": "${env.NDK_HOME}/build/cmake/android.toolchain.cmake", "CMAKE_BUILD_TYPE": "Release", "CMAKE_SYSTEM_NAME": "Android" } } ], "buildPresets": [ { "name": "Android_ARM64_build", "hidden": false, "configurePreset": "Android_ARM64" }, { "name": "Android_ARM32_build", "hidden": false, "configurePreset": "Android_ARM32" } ] }
 
 
总的来说还是推荐第二种方式,有个好处就是vscode在安装了cmake插件会帮我识别头文件,打开工程不会出现头文件引入爆红之类的问题,也可以进行快速引用(vscode的引用当然比不上专业的vsstudio,但是一般也算是够用的)