如何在Make中使用ccache?

我有一个源目录,它使用makefile来编译代码。 此makefile / configure文件不是为ccache兼容性而编写的。 所以我想用ccache。 我在.bashrc中创建了alias alias gcc='ccache gcc' ,但Makefile仍未考虑gcc的这个定义。 因此,如果不触及Makefile/configure文件,我可以做任何事情,这样它就需要ccache gcc而不是gcc 。 CC =’ccache gcc’./configure也不是一个选项,因为它不要求CC。

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

如精细手册所述 :在包含真实gcc的PATH之前列出的目录中创建一个名为“gcc”的符号链接。 这将导致ccache透明使用,无需对makefile进行任何更改。

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

我不确定你为什么放置了一些你自己提到的限制:这些事情很好,你没有理由避免我理解的那些。 例如,如果autoconf的版本不是很古老,那么使用configure提供不同的CC 将会起作用。 你可以这样做:

 ./configure CC='ccache gcc' 

例如,这会将makefile中的CC的默认值设置为ccache gcc 。 我不知道你的意思是“ 它不要求CC ”。

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

 make CC='ccache gcc' 

这也很好。

将打包的ccache添加到PATH

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

这是一种多function的方法,即:

  • 适用于所有编译器:C,C ++等
  • 更少依赖于CC可以实际的Makefile设置
  • 如果您正在自动化某些内容且目标用户未安装ccache则不会破坏人员的构建

man 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上测试过。