如何在 Android NDK 项目中使用预编译头文件? [英] How to use precompiled headers in Android NDK project?

查看:22
本文介绍了如何在 Android NDK 项目中使用预编译头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个大型 C++ 项目从 Visual Studio 移植到 Android 的 GCC.由于文件数量众多,编译时间非常缓慢.我想设置一个预编译的头文件,但我找到了 GCC 文档 混乱.

我有 stdafx.h 文件,它应该是预编译头文件的基础,并且是所有 .cpp 源中的第一个包含文件.有人知道我需要在 Android.mk 中添加什么来完成这项工作吗?

解决方案

遇到了同样的问题,所以有解决办法.首先,您似乎无法通过更改 android.mk 文件来做到这一点,您应该在 ndk 构建的系统中更改文件,但这并不是很危险 %).该解决方案在 r8b NDK 上进行了测试.所以:

  • 将以下代码添加到/build/core/build-binary.mk 脚本中,在 # Build the sources to object files之前:
<上一页>#precompiled 助手:ifeq ($(TARGET_ARCH_ABI),x86)$(调用 set-src-files-target-cflags,$(LOCAL_PCH),)别的$(调用 set-src-files-target-cflags,$(LOCAL_PCH),-mthumb)万一# 构建 PCH#获取-pch-name = $(strip $(subst ../,__/,$(eval __pch := $1)$(eval __pch := $(__pch:%.h=%.precompiled.h))$(__pch)))ifneq (,$(findstring DPCH,$(call module-get-c++-flags,$(LOCAL_MODULE))))# 将 PCH 构建到 obj 目录中LOCAL_BUILT_PCH := $(call get-pch-name,$(LOCAL_PCH))$(call ndk_log, ___________________________Building pch '$(LOCAL_BUILT_PCH)'___________________________)# 构建 PCH$(调用 compile-cpp-source,$(LOCAL_PCH),$(LOCAL_BUILT_PCH).gch)# 所有的obj文件都依赖于PCH$(foreach src,$(过滤器 $(all_cpp_patterns),$(LOCAL_SRC_FILES)),$($(LOCAL_OBJS_DIR)/$(call get-object-name,$(src)) : $(LOCAL_OBJS_DIR)/$(LOCAL_BUILT_PCH).gch))# 从现在开始使用 PCH 构建文件LOCAL_CPPFLAGS += -Winvalid-pch -include $(LOCAL_BUILT_PCH)# 在包含搜索路径的开头插入 PCH 目录LOCAL_C_INCLUDES := $(LOCAL_OBJS_DIR) $(LOCAL_C_INCLUDES)别的$(call ndk_log, ___________________________NO PCH for this module___________________________)万一

  • 在您的模块的 android.mk 中插入以下行:
<上一页>PCH_FILE := symroot/src/Prefix.hLOCAL_PCH := $(PCH_FILE)LOCAL_CPPFLAGS += -DPCH

所以我们将我们的模块标记为具有带有 -DPCH 编译器标志的预编译头(很棘手,但在应用程序中有很多模块时工作).

大部分解决方案取自这里:http://code.google.com/p/android/issues/detail?id=25412

警告:在我对我的项目完成此操作后,它根本没有给我带来编译时间改进,我发现在某些项目上使用 gcc 预编译头文件时会发生这种情况.我还不能解释清楚.

如果您只想在每个 cpp 文件中包含一些文件,只需将以下行添加到 android.mk:

<上一页>PCH_FILE := $(LOCAL_PATH)/symroot/src/Prefix.hLOCAL_CPPFLAGS += -include $(PCH_FILE)

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?

解决方案

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) 

这篇关于如何在 Android NDK 项目中使用预编译头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆