Looking for a cmake clean command to clear up CMake output

Looking for a cmake clean command to clear up CMake output

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)

CMake official FAQ states:

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.

Looking for a cmake clean command to clear up CMake output

In these days of Git everywhere, you may forget CMake and use git clean -d -f -x, that will remove all files not under source control.

Leave a Reply

Your email address will not be published. Required fields are marked *