Android ndk build无法构建32bit可执行文件

根据我的第一个问题 ,我尝试在我的应用程序中包含fanotify.h标头。 该应用程序基于fsmon (一个使用fanotify系统调用的小应用程序),我为了我的目的更改了一些代码行并创建了一个Android.mk makefile:

 APP_PLATFORM := android-23 TARGET_PLATFORM := android-23 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Enable PIE manually. Will get reset on $(CLEAR_VARS). This # is what enabling PIE translates to behind the scenes. LOCAL_CFLAGS += -fPIE -DHAVE_FANOTIFY=1 -DHAVE_SYS_FANOTIFY=0 LOCAL_LDFLAGS += -fPIE -pie # give module name LOCAL_MODULE := fsmon # include fanotify by api 23: LOCAL_C_INCLUDES := /home/fabian/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm/usr/include # include fanotify by sysroot -> # LOCAL_C_INCLUDES := /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/ # list your C files to compile LOCAL_SRC_FILES := inotify.c fanotify.c util.c main.c # this option will build executables instead of building library for android application. include $(BUILD_EXECUTABLE) 

我需要包含linux/inotify.hlinux/fanotify.h头文件。

在执行ndk-build ,只会ndk-build arm64-v8amips64x86_64可执行文件(在libs / xxx中),导致出现以下错误消息:

 fabian@fabian-ubuntu:~/kernel_android_goldfish/fsmon/fsmon/jni$ ndk-build [arm64-v8a] Compile : fsmon <= inotify.c [arm64-v8a] Compile : fsmon <= fanotify.c [arm64-v8a] Compile : fsmon <= util.c [arm64-v8a] Compile : fsmon  libs/arm64-v8a/fsmon [x86_64] Compile : fsmon <= inotify.c [x86_64] Compile : fsmon <= fanotify.c [x86_64] Compile : fsmon <= util.c [x86_64] Compile : fsmon  libs/x86_64/fsmon [mips64] Compile : fsmon <= inotify.c [mips64] Compile : fsmon <= fanotify.c [mips64] Compile : fsmon <= util.c [mips64] Compile : fsmon  libs/mips64/fsmon [armeabi-v7a] Compile thumb : fsmon <= inotify.c In file included from /home/fabian/kernel_android_goldfish/fsmon/fsmon/jni/inotify.c:9: In file included from /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/signal.h:49: In file included from /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/linux/signal.h:21: In file included from /home/fabian/Android/Sdk/ndk-bundle/platforms/android-9/arch-arm/usr/include/asm/signal.h:82: /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/asm-generic/signal.h:92:21: error: use of undeclared identifier '__BITS_PER_LONG' unsigned long sig[_NSIG_WORDS]; ^ /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/asm-generic/signal.h:25:38: note: expanded from macro '_NSIG_WORDS' #define _NSIG_WORDS (_KERNEL__NSIG / _NSIG_BPW) ^ /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/asm-generic/signal.h:24:19: note: expanded from macro '_NSIG_BPW' #define _NSIG_BPW __BITS_PER_LONG ^ /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/asm-generic/signal.h:94:3: error: typedef redefinition with different types ('struct (anonymous struct at /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/asm-generic/signal.h:91:9)' vs 'unsigned long') } sigset_t; ^ /home/fabian/Android/Sdk/ndk-bundle/platforms/android-9/arch-arm/usr/include/asm/signal.h:20:23: note: previous definition is here typedef unsigned long sigset_t; ^ /home/fabian/Android/Sdk/ndk-bundle/platforms/android-9/arch-arm/usr/include/asm/signal.h:84:8: error: redefinition of 'sigaction' struct sigaction { ^ /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/asm-generic/signal.h:101:8: note: previous definition is here struct sigaction { ^ In file included from /home/fabian/kernel_android_goldfish/fsmon/fsmon/jni/inotify.c:9: In file included from /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/signal.h:49: In file included from /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/linux/signal.h:21: /home/fabian/Android/Sdk/ndk-bundle/platforms/android-9/arch-arm/usr/include/asm/signal.h:97:16: error: redefinition of 'sigaltstack' typedef struct sigaltstack { ^ /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/asm-generic/signal.h:111:16: note: previous definition is here typedef struct sigaltstack { ^ In file included from /home/fabian/kernel_android_goldfish/fsmon/fsmon/jni/inotify.c:9: In file included from /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/signal.h:49: In file included from /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/linux/signal.h:21: /home/fabian/Android/Sdk/ndk-bundle/platforms/android-9/arch-arm/usr/include/asm/signal.h:101:3: error: typedef redefinition with different types ('struct (anonymous struct at /home/fabian/Android/Sdk/ndk-bundle/platforms/android-9/arch-arm/usr/include/asm/signal.h:97:16)' vs 'struct sigaltstack') } stack_t; ^ /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/asm-generic/signal.h:116:3: note: previous definition is here } stack_t; ^ In file included from /home/fabian/kernel_android_goldfish/fsmon/fsmon/jni/inotify.c:9: /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/signal.h:71:9: warning: 'NSIG' macro redefined [-Wmacro-redefined] #define NSIG _NSIG ^ /home/fabian/Android/Sdk/ndk-bundle/platforms/android-9/arch-arm/usr/include/asm/signal.h:19:9: note: previous definition is here #define NSIG 32 ^ In file included from /home/fabian/kernel_android_goldfish/fsmon/fsmon/jni/inotify.c:9: /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/signal.h:79:9: warning: 'SIGRTMIN' macro redefined [-Wmacro-redefined] #define SIGRTMIN (__libc_current_sigrtmin()) ^ /home/fabian/Android/Sdk/ndk-bundle/platforms/android-9/arch-arm/usr/include/asm/signal.h:58:9: note: previous definition is here #define SIGRTMIN 32 ^ In file included from /home/fabian/kernel_android_goldfish/fsmon/fsmon/jni/inotify.c:9: /home/fabian/Android/Sdk/ndk-bundle/sysroot/usr/include/signal.h:80:9: warning: 'SIGRTMAX' macro redefined [-Wmacro-redefined] #define SIGRTMAX (__libc_current_sigrtmax()) ^ /home/fabian/Android/Sdk/ndk-bundle/platforms/android-9/arch-arm/usr/include/asm/signal.h:59:9: note: previous definition is here #define SIGRTMAX _NSIG ^ 5 errors generated. make: *** [/home/fabian/kernel_android_goldfish/fsmon/fsmon/obj/local/armeabi-v7a/objs/fsmon/inotify.o] Error 1 

inotify.c原因包括signal.h头,它重新包含了linux/signal.h而这又是platforms/android-9/arch-arm/usr/include/asm/signal.h ,其中出现错误。 (突然来自android-9)。

我无法弄清楚问题出在哪里..我试图在sysroot中包含include文件夹 – 但是会出现类似的错误。

源文件: inotify.c fanotify.c

您不应该手动指向LOCAL_C_INCLUDES的NDK标头。 这应该自动添加,指向正确的版本。 但是,这里没有实际使用变量APP_PLATFORMTARGET_PLATFORM

而是在文件jni/Application.mk添加APP_PLATFORM := android-23 。 对于所有体系结构,这应该使用正确的平台标头进行构建。

除了@ mstorsjo的答案,您还可以在NDK r14和更新版本中使用统一标头 。 这些是标头的新forms,它始终是最新的,而不是为每个API级别设置不同的标头集。

这比APP_PLATFORM增加的APP_PLATFORM是你的APP_PLAFORM不能高于你的minSdkVersion (参见我们的常见问题文档 )。 由于fanotify是在一个相当旧版本的内核中引入的(对于fanotify_init为fanotify_init ),你应该仍然能够用syscall(__NR_fanotify_init)调用它,即使旧版本的libc中不存在该函数。 尽管如此, ENOSYS始终是一种可能性。

默认情况下,在r14中,您仍然可以获得旧标题的forms。 新的“统一标题”包含您正在寻找的标题。 如果要尝试统一标头,请在Application.mk中设置APP_UNIFIED_HEADERS := true (其他构建系统的设置可以在上面的链接中找到)。

在r15(即将推出的第一个测试版)中,默认值已更改为新标题,并且禁用它们的选项已更改(请参阅r15中的相同文档 )。