如何在Android NDK项目中使用预编译头?

发布于 2024-10-19 19:37:46 字数 351 浏览 2 评论 0原文

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

我有 stdafx.h 文件,它应该是预编译头的基础,并且是所有 .cpp 源中的第一个包含文件。有谁知道我需要向 Android.mk 添加什么才能使其正常工作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-10-26 19:37:46

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

  • 将以下代码添加到 /build/core/build-binary.mk 脚本中,在 # Build thesources 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
    
  • 将以下行插入到模块的 android.mk 中:
    PCH_FILE := symroot/src/Prefix.h
    LOCAL_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.h
    LOCAL_CPPFLAGS += -include $(PCH_FILE) 

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) 

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文