如何在Make中使用ccache?

pee*_*ush 3 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编译.

Mad*_*ist 7

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

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

./configure CC='ccache gcc'
Run Code Online (Sandbox Code Playgroud)

例如,它将设置CCmakefile中的默认值ccache gcc.我不知道你的意思是" 它不要求CC ".

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

make CC='ccache gcc'
Run Code Online (Sandbox Code Playgroud)

这也很好.


slo*_*dog 5

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


Cir*_*四事件 5

添加打包好的ccache到PATH

PATH="/usr/lib/ccache:${PATH}" make
Run Code Online (Sandbox Code Playgroud)

这是一种通用方法,即:

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

提到于man ccache

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

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

ls -l /usr/lib/ccache
Run Code Online (Sandbox Code Playgroud)

其中包含大量可能的 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
Run Code Online (Sandbox Code Playgroud)

在 Ubuntu 16.04 上测试。