Android 不能在带有 Android.mk 的 C 程序中包含 linux

Android can't include linux in C program with Android.mk

我正在尝试为 Android6 编译一个 C 程序。这是我的 Android.mk:

APP_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
# 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)

fanotify.c下面的include是这样写的:

#include <linux/fanotify.h>

当我尝试使用 ndk-build 时,出现以下错误:

fsmon/jni/fanotify.c:51:10: fatal error: 'linux/fanotify.h' file not found
#include <linux/fanotify.h>
         ^

header fanotify.h 存在于 ndk 路径中 /Android/Sdk/ndk-bundle/sysroot/usr/include/linux

有什么建议吗?

编辑:如果我尝试包含 sys/fanotify.h

,也会出现同样的错误

您可以使用 LOCAL_C_INCLUDES.

为您的模块指定额外的包含路径

LOCAL_C_INCLUDES := /Android/Sdk/ndk-bundle/sysroot/usr/include/

https://developer.android.com/ndk/guides/android_mk.html#mdv

NDK 历史上没有向后移植 headers 到旧版本,但我们在 r14 中重新设计了一些东西,所以这是可能的:https://android.googlesource.com/platform/ndk/+/ndk-r14-release/docs/UnifiedHeaders.md

默认情况下,在 r14 中您仍然会得到 headers 的旧形式。新 "unified headers" 有您正在寻找的 headers。如果你想尝试统一 headers,请在你的 Application.mk 中设置 APP_UNIFIED_HEADERS := true(其他构建系统的设置可以在上面的 link 中找到)。

在 r15 中(第一个测试版即将推出),默认设置已更改为新的 headers,并且禁用它们的选项已更改(有关选项更改,请参阅 r15 中的同一文档:https://android.googlesource.com/platform/ndk/+/ndk-r15-release/docs/UnifiedHeaders.md).