C++ 如何在 CMake 中使用 CCache?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1815688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Use CCache with CMake?
提问by amit
I would like to do the following: If CCache is present in PATH, use "ccache g++" for compilation, else use g++. I tried writing a small my-cmake script containing
我想执行以下操作:如果 PATH 中存在 CCache,请使用“ccache g++”进行编译,否则使用 g++。我尝试编写一个小的 my-cmake 脚本,其中包含
CC="ccache gcc" CXX="ccache g++" cmake $*
but it does not seem to work (running make still does not use ccache; I checked this using CMAKE_VERBOSE_MAKEFILE on).
但它似乎不起作用(运行 make 仍然不使用 ccache;我使用 CMAKE_VERBOSE_MAKEFILE 进行了检查)。
Update:
更新:
As per this linkI tried changing my script to
根据此链接,我尝试将脚本更改为
cmake -D CMAKE_CXX_COMPILER="ccache" -D CMAKE_CXX_COMPILER_ARG1="g++" -D CMAKE_C_COMPILER="ccache" -D CMAKE_C_COMPILER_ARG1="gcc" $*
but cmake bails out complaining that a test failed on using the compiler ccache (which can be expected).
但是 cmake 会抱怨使用编译器 ccache 时测试失败(这是可以预期的)。
采纳答案by Nicolás
I personally have /usr/lib/ccache
in my $PATH
. This directory contains loads of symlinks for every possible name the compiler could be called from (like gcc
and gcc-4.3
), all pointing to ccache.
我个人/usr/lib/ccache
在我的$PATH
. 该目录包含大量符号链接,用于编译器可以从中调用的每个可能名称(如gcc
和gcc-4.3
),都指向 ccache。
And I didn't even create the symlinks. That directory comes pre-filled when I install ccache on Debian.
我什至没有创建符号链接。当我在 Debian 上安装 ccache 时,该目录已预先填充。
回答by Babcool
It is now possible to specify ccache as a launcher for compile commands and link commands (since cmake 2.8.0). That works for Makefile and Ninja generator. To do this, just set the following properties :
现在可以将 ccache 指定为编译命令和链接命令的启动器(自 cmake 2.8.0 起)。这适用于 Makefile 和 Ninja 生成器。为此,只需设置以下属性:
find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) # Less useful to do it for linking, see edit2
endif(CCACHE_FOUND)
It is also possible to set these properties only for specific directories or targets.
也可以仅为特定目录或目标设置这些属性。
For Ninja, this is possible since version 3.4. For XCode, Craig Scott gives a workaround in his answer.
对于 Ninja,这从 3.4 版开始是可能的。对于 XCode,Craig Scott 在他的回答中给出了一个解决方法。
Edit : Thanks to uprego and Lekensteyn's comment, I edited the answer to check if ccache is available before using it as launcher and for which generators is it possible to use a compile launcher.
编辑:感谢 uprego 和 Lekensteyn 的评论,我编辑了答案以检查 ccache 在用作启动器之前是否可用以及哪些生成器可以使用编译启动器。
Edit2: @Emilio Cobos recommended to avoid doing that for the linking part as ccache doesn't improve linking speed and can mess with other types of cache like sccache
Edit2:@Emilio Cobos 建议避免在链接部分这样做,因为 ccache 不会提高链接速度,并且可能会与其他类型的缓存(如 sccache)混淆
回答by Jacob
As of CMAKE 3.4 you can do:
从 CMAKE 3.4 开始,您可以执行以下操作:
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache
回答by Craig Scott
From CMake 3.1, it is possible to use ccache with the Xcode generator and Ninja is supported from CMake 3.4 onwards. Ninja will honour RULE_LAUNCH_COMPILE
just like the Unix Makefiles generator (so @Babcool's answer gets you there for Ninja too), but getting ccache working for the Xcode generator takes a little more work. The following article explains the method in detail, focussing on a general implementation which works for all three CMake generators and making no assumptions about setting up ccache symlinks or the underlying compiler used (it still lets CMake decide the compiler):
从 CMake 3.1 开始,可以将 ccache 与 Xcode 生成器一起使用,并且从 CMake 3.4 开始支持 Ninja。Ninja 会RULE_LAUNCH_COMPILE
像 Unix Makefiles 生成器一样受到尊重(所以 @Babcool 的回答也能让你为 Ninja 提供帮助),但是让 ccache 为 Xcode 生成器工作需要更多的工作。以下文章详细解释了该方法,重点介绍适用于所有三个 CMake 生成器的一般实现,并且不对设置 ccache 符号链接或使用的底层编译器做任何假设(它仍然让 CMake 决定编译器):
https://crascit.com/2016/04/09/using-ccache-with-cmake/
https://crascit.com/2016/04/09/using-ccache-with-cmake/
The general gist of the article is as follows. The start of your CMakeLists.txt
file should be set up something like this:
文章的大致要点如下。CMakeLists.txt
文件的开头应该设置如下:
cmake_minimum_required(VERSION 2.8)
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
# Support Unix Makefiles and Ninja
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()
project(SomeProject)
get_property(RULE_LAUNCH_COMPILE GLOBAL PROPERTY RULE_LAUNCH_COMPILE)
if(RULE_LAUNCH_COMPILE AND CMAKE_GENERATOR STREQUAL "Xcode")
# Set up wrapper scripts
configure_file(launch-c.in launch-c)
configure_file(launch-cxx.in launch-cxx)
execute_process(COMMAND chmod a+rx
"${CMAKE_BINARY_DIR}/launch-c"
"${CMAKE_BINARY_DIR}/launch-cxx")
# Set Xcode project attributes to route compilation through our scripts
set(CMAKE_XCODE_ATTRIBUTE_CC "${CMAKE_BINARY_DIR}/launch-c")
set(CMAKE_XCODE_ATTRIBUTE_CXX "${CMAKE_BINARY_DIR}/launch-cxx")
set(CMAKE_XCODE_ATTRIBUTE_LD "${CMAKE_BINARY_DIR}/launch-c")
set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${CMAKE_BINARY_DIR}/launch-cxx")
endif()
The two script template files launch-c.in
and launch-cxx.in
look like this (they should be in the same directory as the CMakeLists.txt
file):
这两个脚本模板文件launch-c.in
和launch-cxx.in
看起来像这样(他们应该在同一个目录中的CMakeLists.txt
文件):
launch-c.in:
启动-c.in:
#!/bin/sh
export CCACHE_CPP2=true
exec "${RULE_LAUNCH_COMPILE}" "${CMAKE_C_COMPILER}" "$@"
launch-cxx.in:
启动-cxx.in:
#!/bin/sh
export CCACHE_CPP2=true
exec "${RULE_LAUNCH_COMPILE}" "${CMAKE_CXX_COMPILER}" "$@"
The above uses RULE_LAUNCH_COMPILE
alone for Unix Makefiles and Ninja, but for the Xcode generator it relies on help from CMake's CMAKE_XCODE_ATTRIBUTE_...
variables support. The setting of the CC
and CXX
user-defined Xcode attributes to control the compiler command and LD
and LDPLUSPLUS
for the linker command is not, as far as I can tell, a documented feature of Xcode projects, but it does seem to work. If anyone can confirm it is officially supported by Apple, I'll update the linked article and this answer accordingly.
以上RULE_LAUNCH_COMPILE
单独用于 Unix Makefiles 和 Ninja,但对于 Xcode 生成器,它依赖于来自 CMake 的CMAKE_XCODE_ATTRIBUTE_...
变量支持的帮助。的设置CC
和CXX
用户自定义的Xcode的属性来控制编译器指令,LD
并且LDPLUSPLUS
对连接器的命令是没有的,据我所知,Xcode的项目的记录功能,但它似乎工作。如果有人可以确认它得到了 Apple 的正式支持,我将相应地更新链接的文章和此答案。
回答by jonas
I didn't like to set a symlink from g++
to ccache
. And CXX="ccache g++"
didn't work for me as some cmake test case wanted to have just the compiler program without attributes.
我不喜欢从g++
to设置符号链接ccache
。并且CXX="ccache g++"
对我不起作用,因为某些 cmake 测试用例想要只有没有属性的编译器程序。
So I used a small bash script instead:
所以我改用了一个小的 bash 脚本:
#!/bin/bash
ccache g++ "$@"
and saved it as an executable in /usr/bin/ccache-g++
.
并将其保存为/usr/bin/ccache-g++
.
Then C configured cmake to use /usr/bin/ccache-g++
as C++ compiler.
This way it passes the cmake test cases and I feel more comfortable than having symlinks that I might forget about in 2 or 3 weeks and then maybe wonder if something doesn't work...
然后 C 将 cmake 配置为/usr/bin/ccache-g++
用作 C++ 编译器。这样它就通过了 cmake 测试用例,我感觉比拥有符号链接更舒服,我可能会在 2 或 3 周内忘记这些符号链接,然后可能想知道是否有些东西不起作用......
回答by amit
I verified the following works (source: this link):
我验证了以下作品(来源:此链接):
CC="gcc" CXX="g++" cmake -D CMAKE_CXX_COMPILER="ccache" -D CMAKE_CXX_COMPILER_ARG1="g++" -D CMAKE_C_COMPILER="ccache" -D CMAKE_C_COMPILER_ARG1="gcc" $*
Update: I later realized that even this does not work. Strangely it works every alternate time (the other times cmake complains).
更新:我后来意识到即使这样也行不通。奇怪的是,它每隔一段时间都可以工作(其他时候 cmake 会抱怨)。
回答by Nadir SOUALEM
In my opinion the best way is to symlink gcc,g++ to ccache, but if you would like to use within cmake, try this:
在我看来,最好的方法是将 gcc,g++ 符号链接到 ccache,但如果您想在 cmake 中使用,请尝试以下操作:
export CC="ccache gcc" CXX="ccache g++" cmake ...
回答by Jürgen Weigert
Let me add one important item that was not mentioned here before.
让我补充一个之前没有提到的重要项目。
While bootstrapping a minimalistic build system from the ubunutu:18.04 docker image, I've found that order of installation makes a difference.
在从 ubunutu:18.04 docker 镜像引导一个简约的构建系统时,我发现安装顺序有所不同。
In my case ccache worked fine when calling gcc
, but failed to catch invocations of the same compiler by the other names: cc
and c++
.
To fully install ccache, you need to make sure all compilers are installed first, oradd a call to update-ccache symlinks to be safe.
在我的情况下, ccache 在调用 时工作正常gcc
,但未能通过其他名称捕获相同编译器的调用:cc
和c++
。要完全安装 ccache,您需要确保首先安装所有编译器,或者添加对 update-ccache 符号链接的调用以确保安全。
sudo apt-get install ccache build-essential # and everyhting ...
sudo /usr/sbin/update-ccache-symlinks
export PATH="/usr/lib/ccache/:$PATH"
sudo apt-get install ccache build-essential # and everyhting ...
sudo /usr/sbin/update-ccache-symlinks
export PATH="/usr/lib/ccache/:$PATH"
... and then (due to updated symlinks) also calls to cc and c++ get caught!
...然后(由于更新的符号链接)也调用 cc 和 c++ 被捕获!