正如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主要生成一个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
其他回答
在Git无处不在的今天,你可能会忘记CMake,而使用Git clean -d -f -x,这将删除所有不在源代码控制下的文件。
创建一个临时构建目录,例如build_cmake。因此所有的构建文件都在这个文件夹中。
然后在主CMake文件中添加以下命令。
add_custom_target(clean-all
rm -rf *
)
因此,在编译时
cmake ..
和清洁做:
make clean-all
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.
我同意外部构建是最好的答案。但是当你必须在源代码内构建的时候,我写了一个Python脚本,可以在这里使用,它:
运行“make clean” 在顶级目录中删除特定的cmake生成的文件,例如CMakeCache.txt 对于每个包含CMakeFiles目录的子目录,它会删除CMakeFiles, Makefile, cmake_install.cmake。 删除所有空子目录。
当你在生成build文件时将-D参数传递给CMake,并且不想删除整个build/目录时:
简单地删除构建目录中的CMakeFiles/目录。
rm -rf CMakeFiles/
cmake --build .
这将导致CMake重新运行,并重新生成构建系统文件。您的构建也将从头开始。