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

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


当前回答

从CMake 3.24开始,存在——fresh命令行选项,每次重新构建整个构建树:

——新鲜 3.24新版功能。 执行构建树的新配置。这将删除任何 现有的CMakeCache.txt文件和关联的CMakeFiles/目录 从头开始重新创建。

https://cmake.org/cmake/help/latest/manual/cmake.1.html#options

其他回答

我在谷歌上搜索了大约半个小时,我想到的唯一有用的东西是调用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。X

CMake 3。X提供了一个“干净”的目标。

cmake --build C:/foo/build/ --target clean

来自3.0.2版本的CMake文档:

--clean-first  = Build target 'clean' first, then build.
                 (To clean only, use --target 'clean'.)

CMake 2。X

在cmake版本2中没有cmake clean。X

我通常在一个类似“build”的文件夹中构建项目。如果我想弄清楚,我可以rm -rf构建。

在与根目录“CMakeLists.txt”相同的目录中的“build”文件夹通常是一个不错的选择。要构建你的项目,你只需要把CMakeLists.txt文件的位置作为参数给cmake。例如:cd <location-of-cmakelists>/build && cmake…(从@ComicSansMS)

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

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

如果你跑了

cmake .

它将重新生成CMake文件。如果您将一个新文件添加到由*选择的源文件夹中,这是必需的。比如Cc。

虽然这本身不是“清理”,但它通过重新生成缓存来“清理”CMake文件。