正如make clean删除makefile生成的所有文件一样,我想对CMake做同样的事情。我经常发现自己手动地在目录中删除像cmake_install这样的文件。cmake和CMakeCache.txt,以及CMakeFiles文件夹。

是否有像cmake clean这样的命令来自动删除所有这些文件?理想情况下,这应该遵循当前目录的CMakeLists.txt文件中定义的递归结构。


当前回答

我同意外部构建是最好的答案。但是当你必须在源代码内构建的时候,我写了一个Python脚本,可以在这里使用,它:

运行“make clean” 在顶级目录中删除特定的cmake生成的文件,例如CMakeCache.txt 对于每个包含CMakeFiles目录的子目录,它会删除CMakeFiles, Makefile, cmake_install.cmake。 删除所有空子目录。

其他回答

我同意外部构建是最好的答案。但是当你必须在源代码内构建的时候,我写了一个Python脚本,可以在这里使用,它:

运行“make clean” 在顶级目录中删除特定的cmake生成的文件,例如CMakeCache.txt 对于每个包含CMakeFiles目录的子目录,它会删除CMakeFiles, Makefile, cmake_install.cmake。 删除所有空子目录。

当然,外部构建是Unix makefile的首选方法,但如果您使用的是其他生成器,如Eclipse CDT,它更喜欢您构建内源。在这种情况下,您需要手动清除CMake文件。试试这个:

find . -name 'CMakeCache.txt' -o -name '*.cmake' -o -name 'Makefile' -o -name 'CMakeFiles' -exec rm -rf {} +

或者,如果你已经通过shop -s globstar启用了globstar,试试这个不那么恶心的方法:

rm -rf **/CMakeCache.txt **/*.cmake **/Makefile **/CMakeFiles

在Git无处不在的今天,你可能会忘记CMake,而使用Git clean -d -f -x,这将删除所有不在源代码控制下的文件。

试着使用: cmake——clean-first path-of-CMakeLists.txt-file -B output-dir

-clean-first:先建造目标,然后再建造。 (只清洁,使用-目标清洁。)

我用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