番禺网站建设怎么样单页设计风格
windows+cmake+vscode+NDK远程调试安卓端C++项目
- windows+cmake+vscode+NDK远程调试安卓端C++项目
windows+cmake+vscode+NDK远程调试安卓端C++项目
使用C++开发安卓端算法库时,需要使用NDK进行交叉编译。使用NDK编译代码时,需要编写.mk脚本进行代码的编译和链接,比较繁琐,因此可以借助cmake和NDK工具链组织项目,只需要在根目录的CMakeLists.txt中调用NDK工具链就可以进行交叉编译,无需学习NDK编译脚本的语法,并且仍然可以发挥cmake 的特长。
在这片文章中,我们不介绍如何使用cmake+NDK进行交叉编译,认为读者已经有这方面的基础。这部分相关的内容可以阅读这个系列中的其他博文。
本文主要介绍如何使用NDK的调试工具,在windows上调试C++项目,而C++项目实际是在安卓手机上运行的。
- 假设我们已经有了一个完整的C++ + cmake +NDK编译的项目。
- 编写一个build.bat脚本,用于自动进行cmake构建
@echo off
if exist "./build" (rmdir /s /q "./build"echo Folder deleted successfully.
) else (echo Folder not found.
)
set current_path=%cd%@REM call cmake --no-warn-unused-cli -DCONFIG=android_qcom_arm64 -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -S . -B ./build -DCMAKE_MAKE_PROGRAM=%current_path%\ninja\ninja -G "Ninja"
call cmake --no-warn-unused-cli -DCONFIG=android_qcom_arm64 -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -S . -B ./build -DCMAKE_MAKE_PROGRAM="D:\msys64\mingw64\bin\mingw32-make.exe" -G "MinGW Makefiles"
call cmake --build ./build --config Debug --target all --
脚本中的路径自行替换
- 编写run.bat脚本将编译后的库文件和可执行文件push到手机端,同时将gdb工具push到手机端。其中gdb工具位于NDK安装目录下“android-ndk-r23/prebuilt/android-arm64/gdbserver/gdbserver”。
adb root
adb remountadb shell rm -rf /data/tmp/
adb shell mkdir /data/tmp/
adb push ./build/bin/testbed /data/tmp/
adb push ./build/sharedLib /data/tmp/
adb push "D:/android-ndk-r23/prebuilt/android-arm64/gdbserver/gdbserver" /data/tmp/adb shell ls /data/tmp/
adb shell chmod -v 777 /data/tmp/testbed
adb shell chmod -v 777 /data/tmp/gdbserveradb forward tcp:9090 tcp:9090set env=export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/tmp/sharedLib
@REM adb shell "%env% && /data/tmp/testbed"
adb shell "%env% && /data/tmp/gdbserver :9090 /data/tmp/testbed"
- -在工程中创建.vscode文件夹,在该文件夹下创建launch.json和task.json这两个文件t。task.json文件内容如下,其中上述两个bat脚本都放在工程的根目录下
{"version": "2.0.0","tasks": [{"label": "build","type": "shell","command": "cmd","args": ["/c","${workspaceFolder}/build.bat"],"group": {"kind": "build","isDefault": true}},{"label": "run","dependsOn": ["build"], // 指定要依赖的任务列表"type": "shell","command": "start cmd","args": ["/c","${workspaceFolder}/run.bat"],"group": {"kind": "build","isDefault": true}},]
}
launch.json文件内容如下:其中在"program": “${workspaceFolder}/build/bin/testbed”,指定本地可执行文件的路径;在"miDebuggerPath": “D:/android-ndk-r23/prebuilt/windows-x86_64/bin/gdb.exe”,指定本地NDK的gdb路径
{"version": "0.2.0","configurations": [{"name": "Remote Debug","type": "cppdbg","request": "launch","program": "${workspaceFolder}/build/bin/testbed","args": [],"stopAtEntry": true,"cwd": "${workspaceFolder}","environment": [],"externalConsole": true,"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"serverLaunchTimeout": 1000,"filterStderr": true,"filterStdout": true,"serverStarted": "GDB server started on port","miDebuggerPath": "D:/android-ndk-r23/prebuilt/windows-x86_64/bin/gdb.exe","miDebuggerServerAddress": ":9090","preLaunchTask": "run",}]
}
启动vscode中的运行->启动调试开始调试程序,此时会弹出一个外部的windows终端。这样一来就可以在windows上打断点,查看变量了。但是代码实际上是在手机上运行,只是通过gdbserver将调试信息从手机端发送到vscode上。