33

I am using CMake to generate the project files.

Is there a command with which I can clean up all those generated project files?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
  • 2
    If it's a git repository, `git checkout . & git clean -dxf` will wipe everything clean to exactly the state of your git index. – Brent Jul 01 '17 at 20:47
  • I also looked for `cmake clean` command yesterday, found none. But now my question is why are there projects still building in root source folder instead of `build`? :) – Tien Do Sep 01 '18 at 06:35

1 Answers1

54

Is there a command with which I can clean up all those generated project files?

Yes - if you do an out-of-source build (as recommended).

To do so, create a directory - for example, build - within your projects directory and run CMake from there:

mkdir build
cd build
cmake ..
make

(Or tell your IDE to do so.)

Now all generated files are within build. To clean those, just remove the directory (rm -rf build).


PRO: You can keep all generated files out of your versioning system by adding build to its ignore ;-)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ollo
  • 24,797
  • 14
  • 106
  • 155
  • 5
    Just for completeness: There's no way to do it automatically in-source, is that correct? – Michael Jul 20 '16 at 08:22
  • 4
    if you call cmake from the root directory of your project it's in-source – all build files are then scattered into your project. Doing an out-of-source build will keep them within one place (eg. `build/`). So it's more an issue of how you use cmake. Did i get your question right? – ollo Jul 22 '16 at 18:11
  • This breaks Conan and invites non-portable deletion commands and doesn't integrate well with automation and relies subtle directory changes that cause all sorts of problems. – mcandre Apr 16 '23 at 04:03