对于 C 应用程序的编译链接,自己编写的 makefile 是件比较困难的事情,我们可以利用 NDK 提供的 build 工具,实现我们的目标。
比如我们以hello.c工程:
#includeint main() { printf("Hello World!\n"); return 0; }
编写Android.mk文件
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE:= helloaLOCAL_SRC_FILES := hello.cinclude $(BUILD_EXECUTABLE)
在当前目录下打开终端运行:ndk-build,出现如下错误:
[D:\ndk\android-ndk-r9d\sources\hellotest]$ ndk-buildAndroid NDK: Could not find application project directory ! Android NDK: Please define the NDK_PROJECT_PATH variable to point to it. D:\ndk\android-ndk-r9d\build/core/build-local.mk:148: *** Android NDK: Aborting . Stop.
解决办法有两种:
1)在该路径下创建一个空白的AndroidManifest.xml文件
再次执行ndk-build,可顺利编译出可执行文件,adb push到android平台上执行:
[D:\ndk\android-ndk-r9d\sources\hellotest]$ ndk-build[armeabi] Compile thumb : helloa <= hello.c[armeabi] Executable : helloa[armeabi] Install : helloa => libs/armeabi/helloa
2)根据提示,可进行NDK_PROJECT_PATH赋值指定:
ndk-build -B NDK_PROJECT_PATH=.
再次执行ndk-build,同样可顺利编译出可执行文件,adb push到android平台上执行:
[D:\ndk\android-ndk-r9d\sources\hellotest]$ ndk-build[armeabi] Compile thumb : helloa <= hello.c[armeabi] Executable : helloa[armeabi] Install : helloa => libs/armeabi/helloa