正如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文件中定义的递归结构。
当前回答
简单地发布rm CMakeCache.txt也适用于我。
其他回答
清除cmake构建输出:
命令行:
$ rm -rf [folder that you builded the project]/
$ cmake --build .
Cmake:
cmake --build . --target clean
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-build-debug文件夹,当你使用cmake编译时,它会自动创建一个新的cmake-build-debug文件夹,里面有你需要的所有东西。尤其适用于CLion。
为了简化使用“源外”构建时的清理(即在构建目录中构建),我使用以下脚本:
$ cat ~/bin/cmake-clean-build
#!/bin/bash
if [ -d ../build ]; then
cd ..
rm -rf build
mkdir build
cd build
else
echo "build directory DOES NOT exist"
fi
每次你需要清理时,你应该从build目录中获取这个脚本:
. cmake-clean-build
CMake 3。X
CMake 3.0及以上版本提供了一个“干净”的目标。这将删除任何工件,如目标文件、库文件、可执行文件、生成文件等。
cmake --build C:/foo/build/ --target clean
您还可以清理构建,然后运行构建。在1命令。
cmake --build C:/foo/build --clean-first
但是,这不会清理CMakeCache.txt或相关的CMakeFiles/目录。你可能想这么做。你只需要删除构建文件夹。
# Just delete the build folder
rm C:/foo/build -rf
# You can also just let git delete the build folder as well
git clean -d -f -x
CMake 3.24
现在在CMake 3.24中,你可以执行一个新的构建树配置。这将删除任何现有的CMakeCache.txt文件和相关的CMakeFiles/目录,并从头开始重新创建它们。
一般情况下,你需要这样做:
你想清除CMakeCache.txt中的缓存变量 您希望更改编译器 任何与CMake缓存相关的其他操作
cmake -B C:/foo/build --fresh