有同样的问题,所以有一个解决方案.首先,因为似乎你不能通过更改android.mk文件来做到这一点,你应该在ndk内置系统中更改文件,但这不是很危险的%).该溶液在r8b NDK上测试.所以:
>将以下代码添加到/build/core/build-binary.mk脚本之前#将源构建为对象文件:
#precompiled helper:
ifeq ($(TARGET_ARCH_ABI),x86)
$(call set-src-files-target-cflags,$(LOCAL_PCH),)
else
$(call set-src-files-target-cflags,$(LOCAL_PCH),-mthumb)
endif
# Build PCH
#
get-pch-name = $(strip \
$(subst ../,__/,\
$(eval __pch := $1)\
$(eval __pch := $(__pch:%.h=%.precompiled.h))\
$(__pch)\
))
ifneq (,$(findstring DPCH,$(call module-get-c++-flags,$(LOCAL_MODULE))))
# Build PCH into obj directory
LOCAL_BUILT_PCH := $(call get-pch-name,$(LOCAL_PCH))
$(call ndk_log, ___________________________Building pch '$(LOCAL_BUILT_PCH)'___________________________)
# Build PCH
$(call compile-cpp-source,$(LOCAL_PCH),$(LOCAL_BUILT_PCH).gch)
# All obj files are dependent on the PCH
$(foreach src,$(filter $(all_cpp_patterns),$(LOCAL_SRC_FILES)),\
$($(LOCAL_OBJS_DIR)/$(call get-object-name,$(src)) : $(LOCAL_OBJS_DIR)/$(LOCAL_BUILT_PCH).gch)\
)
# Files from now on build with PCH
LOCAL_CPPFLAGS += -Winvalid-pch -include $(LOCAL_BUILT_PCH)
# Insert PCH dir at beginning of include search path
LOCAL_C_INCLUDES := \
$(LOCAL_OBJS_DIR) \
$(LOCAL_C_INCLUDES)
else
$(call ndk_log, ___________________________NO PCH for this module___________________________)
endif
>将以下行插入到您的模块的android.mk中:
PCH_FILE := symroot/src/Prefix.h
LOCAL_PCH := $(PCH_FILE)
LOCAL_CPPFLAGS += -DPCH
因此,我们将模块标记为具有-DPCH编译器标志的预编译头(棘手,但在应用程序中有很多模块时工作).
警告:在我完成这个项目之后,它并没有给我编译时间改进,我发现这是在一些项目的gcc预编译头.不能解释自己这个.
如果您只想在每个cpp文件中添加一些文件,只需将以下行添加到android.mk中即可:
PCH_FILE := $(LOCAL_PATH)/symroot/src/Prefix.h
LOCAL_CPPFLAGS += -include $(PCH_FILE)