Debugging native code with ndk-gdb using standalone CMake toolchain

I recently ran into this problem and could not find any good solution on the Internet. So next comes a small summary of the problem with hopefully enough buzzwords, so Google can lead you here.

If you want to do C++ development on Android, you need the NDK for cross compilation. It comes by default with its own build system called ndk-build, which basically is a bunch of custom makefiles. But if you are sharing code between the Android Platform and lets say plain Linux, you have likely already a build system installed. For C/C++ CMake is quite popular as it supports different platforms and compilers. Fortunately there is already a project which adds Android support to CMake. I will not cover that – instead I assume you are using it already.

Unfortunately you cant use the ndk-gdb script supplied with the NDK to debug your application as it relies on the behaviour of ndk-build. But as said earlier, ndk-build is no wizardy, but just a bunch of scripts. So it is possible to emulate the behaviour using CMake, as following:

Add the following lines to your CMakeLists.txt file

set(LIBRARY_NAME myNativeLib)
add_library(${LIBRARY_NAME} SHARED ...)

# 1. generate
file(WRITE ./jni/ "APP_ABI := ${ANDROID_NDK_ABI_NAME}\n")

# 2. generate gdb.setup
file(WRITE ./libs/${ANDROID_NDK_ABI_NAME}/gdb.setup "set solib-search-path ./obj/local/${ANDROID_NDK_ABI_NAME}\n")
file(APPEND ./libs/${ANDROID_NDK_ABI_NAME}/gdb.setup "directory ${INCLUDE_DIRECTORIES}\n")

# 3. copy gdbserver executable
file(COPY ${ANDROID_NDK}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/gdbserver DESTINATION ./libs/${ANDROID_NDK_ABI_NAME}/)

# 4. copy lib to obj
add_custom_command(TARGET ${LIBRARY_NAME} POST_BUILD COMMAND mkdir -p ./obj/local/${ANDROID_NDK_ABI_NAME}/)

# 5. strip symbols

You should now be able to use ndk-gdb with CMake, just as if you would have used ndk-build.

Note that steps 4 and 5 are optional for debugging. They just reduce the size of the library that has to be transferred to the device. If you dont care, you can just leave them out. But then the solib search path from step 2 must be set to:

file(WRITE ./libs/${ANDROID_NDK_ABI_NAME}/gdb.setup "set solib-search-path ./libs/${ANDROID_NDK_ABI_NAME}\n")

Ideally someone should integrate that in the Android toolchain linked above, so that these steps are performed automatically…

  • iwasz

    Thanks for this info. It was very helpful for me. Thumbs up!

  • Dave

    Thanks heaps for this. Was just looking for the strip command, but the rest would be awesome as well