如何在Make中使用ccache?

时间:2012-03-18 10:02:21

标签: c makefile ccache

我有一个使用makefile编译代码的源目录。此makefile / configure文件不是为ccache兼容性而编写的。所以我想用ccache。我在.bashrc中创建了alias gcc='ccache gcc'的别名,但是Makefile仍然没有考虑gcc的这个定义。因此,如果不触及Makefile/configure文件,我可以执行任何操作,以便ccache gcc代替gcc。 CC ='ccache gcc'./configure也不是一个选项,因为它不要求CC。

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

3 个答案:

答案 0 :(得分:7)

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

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

./configure CC='ccache gcc'
例如

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

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

make CC='ccache gcc'

也可以。

答案 1 :(得分:5)

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

答案 2 :(得分:0)

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

相关问题