Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use precompiled headers in Android NDK project?

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?

like image 574
Meh Avatar asked Mar 01 '11 05:03

Meh


People also ask

How do you use precompiled headers?

The compiler options for precompiled headers are /Y . In the project property pages, the options are located under Configuration Properties > C/C++ > Precompiled Headers. You can choose to not use precompiled headers, and you can specify the header file name and the name and path of the output file.

Are precompiled headers worth it?

The advantage of precompiled headers is that the huge system library header files and other files that do not change at all or infrequently only have to be parsed once per build. As all C compilers (that I know of) work on every .

What is a precompiled header C++?

In computer programming, a precompiled header (PCH) is a (C or C++) header file that is compiled into an intermediate form that is faster to process for the compiler.

What compiler does NDK use?

Code written in C/C++ can be compiled to ARM, or x86 native code (or their 64-bit variants) using the Android Native Development Kit (NDK). The NDK uses the Clang compiler to compile C/C++. GCC was included until NDK r17, but removed in r18 in 2018.


1 Answers

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) 

like image 137
Sergey Leyko Avatar answered Oct 05 '22 23:10

Sergey Leyko