Looking for a ‘cmake clean’ command to clear up CMake output

cmake

Just as make clean deletes all the files that a makefile has produced, I would like to do the same with CMake. All too often I find myself manually going through directories removing files like cmake_install.cmake and CMakeCache.txt, and the CMakeFiles folders.

Is there a command like cmake clean to remove all these files automatically? Ideally this should follow the recursive structure defined within the current directory's CMakeLists.txt file.

Best Answer

CMake 3.X

CMake 3.X offers a 'clean' target.

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

From the CMake docs for 3.0.2:

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

CMake 2.X

There is no cmake clean in CMake version 2.X

I usually build the project in a single folder like "build". So if I want to make clean, I can just rm -rf build.

The "build" folder in the same directory as the root "CMakeLists.txt" is usually a good choice. To build your project, you simply give cmake the location of the CMakeLists.txt as an argument. For example: cd <location-of-cmakelists>/build && cmake ... (From @ComicSansMS)