How to use precompiled headers in Android NDK project?

怎甘沉沦 提交于 2019-12-18 11:08:12

问题


I'm porting a big C++ project from Visual Studio to GCC for Android. Because of the large number of files, the compile times are glacial. I would like to setup a precompiled header file, but I find the GCC documentation confusing.

I have the stdafx.h file which should be the base of the precompiled headers and which is the first included file in all the .cpp sources. Does anybody know what do I need to add to Android.mk to make this work?


回答1:


Had the same problem, so there is a solution. First of all, as it seems to be you are not able to do it with changing the android.mk files, you should change file in the ndk built system, but this is not very dangerous %). This solution tested on r8b NDK. So:

  • Add the following code to the /build/core/build-binary.mk script, before # Build the sources to object files:

    #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
    
  • Insert the following lines to your android.mk of your module:
    PCH_FILE := symroot/src/Prefix.h
    LOCAL_PCH := $(PCH_FILE)
    LOCAL_CPPFLAGS += -DPCH

So we mark our module as having the precompiled header with -DPCH compilator flag (tricky, but work when there are a lot of modules in the application).

The most of solution is taken from here: http://code.google.com/p/android/issues/detail?id=25412

WARNING: after I have done this with my project, it did not give me compilation time improvement at all, and i found that this happens with gcc precompiled headers on some projects. Can't explain myself this yet.

If you want just include some file into every cpp file, just add the following lines to android.mk:


    PCH_FILE := $(LOCAL_PATH)/symroot/src/Prefix.h
    LOCAL_CPPFLAGS += -include $(PCH_FILE) 



来源:https://stackoverflow.com/questions/5150778/how-to-use-precompiled-headers-in-android-ndk-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!