【问题标题】:How to use ccache selectively?如何有选择地使用ccache?
【发布时间】:2010-04-22 16:28:21
【问题描述】:

我必须编译一个用 C++ 编写的应用程序的多个版本,我想使用 ccache 来加快进程。

ccache howtos 有示例建议创建名为 gcc、g++ 等的符号链接,并确保它们出现在原始 gcc 二进制文件之前的 PATH 中,因此使用 ccache 代替。

到目前为止一切都很好,但我只想在编译这个特定应用程序时使用 ccache,而不是总是。

当然,我可以编写一个 shell 脚本,每次我想编译应用程序时尝试创建这些符号链接,并在编译应用程序时删除它们。但这对我来说似乎是文件系统滥用。

有没有更好的方法来选择性地使用 ccache?

对于单个源代码文件的编译,我可以手动调用 ccache 而不是 gcc 即可完成,但我必须处理一个复杂的应用程序,它使用自动构建系统来编译多个源代码文件。

【问题讨论】:

    标签: linux gcc ccache


    【解决方案1】:

    绕过ccache:

    export CCACHE_DISABLE=1
    

    更多信息:

    man ccache
    

    ...

            If you set the environment variable CCACHE_DISABLE then ccache will just call the real
           compiler, bypassing the cache completely.
    

    ...

    【讨论】:

    • 好吧,它真的不起作用。例如,它不适用于通过脚本配置ccache 的比特币项目。如果我们知道ccaches 缓存文件在哪里,那就太好了,所以我们可以简单地删除它们。与任何其他缓存解决方案一样。
    【解决方案2】:

    什么操作系统? Linux?大多数打包版本的 ccache 已经将这些符号链接放入一个目录中,例如在我的 Fedora 机器上,它们位于 /usr/lib64/ccache 中。

    所以你可以这样做

    PATH=/usr/lib64/ccache:${PATH} make
    

    当你想用 ccache 构建时。

    大多数软件包还会在 /etc/profile.d/ 中安装一个文件,该文件会自动启用 ccache,方法是将其添加到 PATH 中,如上所述。

    如果您的系统出现这种情况,只需在您的环境中设置CCACHE_DISABLE=1(有关更多信息,请参阅man ccache)以禁用 ccache - ccache 仍将运行,但只会调用真正的编译器。

    【讨论】:

      【解决方案3】:

      我现在偶然发现了很多次。对我来说,最好的解决方案是这样做:

      export CCACHE_RECACHE=1;
      

      来自 ccache 手册页:

      Corrupt object files
         It should be noted that ccache is susceptible to general storage problems. If a bad object file sneaks into
         the cache for some reason, it will of course stay bad. Some possible reasons for erroneous object files are
         bad hardware (disk drive, disk controller, memory, etc), buggy drivers or file systems, a bad prefix_command
         or compiler wrapper. If this happens, the easiest way of fixing it is this:
      
          1. Build so that the bad object file ends up in the build tree.
      
          2. Remove the bad object file from the build tree.
      
          3. Rebuild with CCACHE_RECACHE set.
      

      【讨论】:

        【解决方案4】:

        创建符号链接的替代方法是显式使用 ccache gcc 作为 C 编译器,使用 ccache g++ 作为 C++ 编译器。例如,如果您的 Makefile 使用变量 CCCXX 来指定编译器,您可以使用 make CC="ccache gcc" CXX="ccache g++" 构建或在配置时设置它 (./configure CC="ccache gcc" CXX="ccache g++")。

        【讨论】:

          猜你喜欢
          • 2019-03-04
          • 1970-01-01
          • 2010-12-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多