正如make clean删除makefile生成的所有文件一样,我想对CMake做同样的事情。我经常发现自己手动地在目录中删除像cmake_install这样的文件。cmake和CMakeCache.txt,以及CMakeFiles文件夹。
是否有像cmake clean这样的命令来自动删除所有这些文件?理想情况下,这应该遵循当前目录的CMakeLists.txt文件中定义的递归结构。
正如make clean删除makefile生成的所有文件一样,我想对CMake做同样的事情。我经常发现自己手动地在目录中删除像cmake_install这样的文件。cmake和CMakeCache.txt,以及CMakeFiles文件夹。
是否有像cmake clean这样的命令来自动删除所有这些文件?理想情况下,这应该遵循当前目录的CMakeLists.txt文件中定义的递归结构。
当前回答
有趣的是,这个问题得到了如此多的关注和复杂的解决方案,这确实表明了cmake没有一个干净的方法的痛苦。
好吧,你当然可以cd build_work来做你的工作,然后在你需要清理的时候做一个rm -rf *。然而,rm -rf *是一个危险的命令,因为许多人通常不知道他们在哪个目录中。
如果你cd .., rm -rf build_work,然后mkdir build_work,然后CD build_work,输入太多了。
所以一个好的解决方案是远离build文件夹,告诉cmake路径: 配置:cmake -B build_work 要构建:cmake—build build_work 安装方法:cmake—install build_work 清理:rm -rf build_work 重建build文件夹:你甚至不需要mkdir build_work,只需配置cmake -B build_work即可。
其他回答
我用zsxwing的答案成功解决了以下问题:
我有在多个主机上构建的源代码(在Raspberry Pi Linux板上,在VMware Linux虚拟机上,等等)。
我有一个Bash脚本,根据机器的主机名创建临时目录,就像这样:
# Get hostname to use as part of directory names
HOST_NAME=`uname -n`
# Create a temporary directory for cmake files so they don't
# end up all mixed up with the source.
TMP_DIR="cmake.tmp.$HOSTNAME"
if [ ! -e $TMP_DIR ] ; then
echo "Creating directory for cmake tmp files : $TMP_DIR"
mkdir $TMP_DIR
else
echo "Reusing cmake tmp dir : $TMP_DIR"
fi
# Create makefiles with CMake
#
# Note: switch to the temporary dir and build parent
# which is a way of making cmake tmp files stay
# out of the way.
#
# Note 2: to clean up cmake files, it is OK to
# "rm -rf" the temporary directories
echo
echo Creating Makefiles with cmake ...
cd $TMP_DIR
cmake ..
# Run makefile (in temporary directory)
echo
echo Starting build ...
make
我在谷歌上搜索了大约半个小时,我想到的唯一有用的东西是调用find实用程序:
# Find and then delete all files under current directory (.) that:
# 1. contains "cmake" (case-&insensitive) in its path (wholename)
# 2. name is not CMakeLists.txt
find . -iwholename '*cmake*' -not -name CMakeLists.txt -delete
此外,确保在此之前调用make clean(或您正在使用的任何CMake生成器)。
:)
从CMake 3.24开始,存在——fresh命令行选项,每次重新构建整个构建树:
——新鲜 3.24新版功能。 执行构建树的新配置。这将删除任何 现有的CMakeCache.txt文件和关联的CMakeFiles/目录 从头开始重新创建。
https://cmake.org/cmake/help/latest/manual/cmake.1.html#options
cmake主要生成一个Makefile文件,可以将rm添加到干净的PHONY文件中。
例如,
[root@localhost hello]# ls
CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt hello Makefile test
[root@localhost hello]# vi Makefile
clean:
$(MAKE) -f CMakeFiles/Makefile2 clean
rm -rf *.o *~ .depend .*.cmd *.mod *.ko *.mod.c .tmp_versions *.symvers *.d *.markers *.order CMakeFiles cmake_install.cmake CMakeCache.txt Makefile
如果您有自定义定义,并希望在清理之前保存它们,请在构建目录中运行以下命令:
sed -ne '/variable specified on the command line/{n;s/.*/-D \0 \\/;p}' CMakeCache.txt
然后创建一个新的构建目录(或删除旧的构建目录并重新创建它),最后使用上面脚本获得的参数运行cmake。