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

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


当前回答

清除cmake构建输出:

命令行:

$ rm -rf [folder that you builded the project]/
$ cmake --build .

Cmake:

cmake --build . --target clean

其他回答

也许它有点过时,但由于这是你谷歌cmake clean时的第一个热门,我将添加这个:

因为您可以使用指定的目标在生成目录中启动生成

Cmake——构建。——目标xyz

你当然可以跑

Cmake——构建。——目标清洁

在生成的构建文件中运行干净目标。

CMake官方常见问题解答说明:

Some build trees created with GNU autotools have a "make distclean" target that cleans the build and also removes Makefiles and other parts of the generated build system. CMake does not generate a "make distclean" target because CMakeLists.txt files can run scripts and arbitrary commands; CMake has no way of tracking exactly which files are generated as part of running CMake. Providing a distclean target would give users the false impression that it would work as expected. (CMake does generate a "make clean" target to remove files generated by the compiler and linker.) A "make distclean" target is only necessary if the user performs an in-source build. CMake supports in-source builds, but we strongly encourage users to adopt the notion of an out-of-source build. Using a build tree that is separate from the source tree will prevent CMake from generating any files in the source tree. Because CMake does not change the source tree, there is no need for a distclean target. One can start a fresh build by deleting the build tree or creating a separate build tree.

清除cmake构建输出:

命令行:

$ rm -rf [folder that you builded the project]/
$ cmake --build .

Cmake:

cmake --build . --target clean

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

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

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

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