7

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

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

4

3 回答 3

10

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

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

./configure CC='ccache gcc'

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

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

make CC='ccache gcc'

这也可以正常工作。

于 2012-03-18T19:08:11.240 回答
8

As described by the fine manual: Create a symlink named "gcc" in a directory that is listed in your PATH before the one containing the real gcc. This will cause ccache to be used transparently, with no need for any change to the makefile.

于 2012-03-27T16:51:09.753 回答
2

将打包的 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 上测试。

于 2018-09-14T15:25:55.743 回答