【问题标题】:How to use ccache with Make?如何在 Make 中使用 ccache?
【发布时间】:2012-03-18 10:02:20
【问题描述】:

我有一个使用 makefile 编译代码的源目录。此 makefile/配置文件不是为了 ccache 兼容性而编写的。所以我想用ccache。我在 .bashrc 中创建了别名为alias gcc='ccache gcc',但是 Makefile 仍然没有考虑 gcc 的这个定义。那么有什么我可以在不接触Makefile/configure 文件的情况下使用ccache gcc 而不是gcc。 CC='ccache gcc' ./configure 也不是一个选项,因为它不要求 CC。

如果我编写 Makefile,那么我可以提供 ${gcc),但这不是一个选项,因为我不是在编写 Makefile。有什么方法我们不需要更改源文件中的任何内容,但仍然启用ccache 编译。

【问题讨论】:

  • 那么,Makefile 用什么来编译呢?是否硬编码使用gcc 而不是$(CC)
  • Makefile 是由 configure 创建的,而后者又是由 autoconf 创建的,尽管 makefile 会解析 configure.in。但是更改会产生另一个“配置文件已更改”的问题,所以我正在寻找一种方法,在不更改源文件( makefile/configure )的情况下有什么方法可以这样做。

标签: c makefile ccache


【解决方案1】:

别名在创建它们的 shell 中是本地的;与环境变量不同,它们不会传递给 shell 调用的任何程序(包括 make)。 Make 调用 /bin/sh,而不是 /bin/bash,并且 /bin/sh 不会读取您的 ~/.bashrc 等,因此没有定义的别名会对您有所帮助。

我不完全确定您为什么对自己设置了一些您提到的限制:这些东西可以正常工作,而您没有给出避免它们的理由,我理解。例如,如果 autoconf 的版本不是很古老,则为不同的 CC 提供 configure 起作用。你可以这样做:

./configure CC='ccache gcc'

例如,这会将您的 makefile 中 CC 的默认值设置为 ccache gcc。我不知道您所说的“它不要求抄送”是什么意思。

如果您愿意,也可以在 make 命令行上覆盖 CC 的设置,如下所示:

make CC='ccache gcc'

这也很好用。

【讨论】:

  • 这个我已经提到CC选项不是配置。 gcc 在那里被硬编码。换句话说,配置脚本不会寻找 CC 变量。
【解决方案2】:

the fine manual 所述:在包含真正 gcc 的 PATH 中列出的目录中创建一个名为“gcc”的符号链接。这将导致 ccache 被透明地使用,无需对 makefile 进行任何更改。

【讨论】:

    【解决方案3】:

    将打包的 ccache 添加到 PATH

    PATH="/usr/lib/ccache:${PATH}" make
    

    这是一种通用的方法,即:

    • 同时适用于所有编译器:C、C++ 等。
    • 不太依赖于实际的Makefile 设置,例如CC 可以
    • 如果您正在自动化某些东西并且目标用户没有安装 ccache,则不会破坏人们的构建

    提到man ccache:

    要在 Debian 系统上使用第二种方法,最简单的方法是将 /usr/lib/ccache 预先添加到您的 PATH 中。 /usr/lib/ccache 包含当前作为 Debian 安装的所有编译器的符号链接 包。

    您可以通过以下方式确认:

    ls -l /usr/lib/ccache
    

    其中包含大量可能的 GCC 名称,包括已安装的交叉编译器:

    total 0
    lrwxrwxrwx 1 root root 16 May  6 13:51 aarch64-linux-gnu-gcc -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 aarch64-linux-gnu-gcc-7 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 Jun 23 18:25 arm-linux-gnueabi-g++ -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 Jun 23 18:25 arm-linux-gnueabi-g++-7 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 arm-linux-gnueabi-gcc -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 arm-linux-gnueabi-gcc-7 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May 28 22:11 arm-linux-gnueabihf-gcc -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May 28 22:11 arm-linux-gnueabihf-gcc-7 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 arm-none-eabi-g++ -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 arm-none-eabi-gcc -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 arm-none-eabi-gcc-6.3.1 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 c++ -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 c89-gcc -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 c99-gcc -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 cc -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 clang -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 clang++ -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 g++ -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 g++-5 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 g++-6 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 g++-7 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 gcc -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 gcc-5 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 gcc-6 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 gcc-7 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++ -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++-5 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++-6 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-g++-7 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc-5 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc-6 -> ../../bin/ccache
    lrwxrwxrwx 1 root root 16 May  6 13:51 x86_64-linux-gnu-gcc-7 -> ../../bin/ccache
    

    在 Ubuntu 16.04 上测试。

    【讨论】:

      猜你喜欢
      • 2010-12-21
      • 2012-12-24
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 2015-04-16
      相关资源
      最近更新 更多