When cmake encounters an error, sometimes it does not stop instantly but continues to process all CMake files. It is only at the end of the treatment that we learn that there is an error, so we have to go back up the whole log to see where it is located.

Is there any option, variable or means to stop CMake just after the first error?

Clarification: I'm not trying to stop the compilation of source code but the CMake generation (e.g. on Linux the generation of the Makefiles).

Here is an example (this is not my real project, this is specifically designed to generate errors). My file CMakeLists.txt:

cmake_minimum_required(VERSION 3.6)
set_property(TARGET foo PROPERTY PROP1 TRUE)
set_property(TARGET bar PROPERTY PROP2 TRUE)

And when i run cmake . I get the output :

CMake Error at CMakeLists.txt:3 (set_property):
  set_property could not find TARGET foo.  Perhaps it has not yet been

CMake Error at CMakeLists.txt:4 (set_property):
  set_property could not find TARGET bar.  Perhaps it has not yet been

-- Configuring incomplete, errors occurred!

What I would like to know is if cmake has the possibility to stop just after the first reported error.

  • 1
    Have you tried -Werror=dev? Or, for errors you produce yourself, if(ERR_COND) message( FATAL_ERROR "Fatal error, exiting." )? Aug 12, 2020 at 9:05
  • 1
    @DmitryGrigoryev Thanks for your answer. The option does not change the behaviour on this test project, as there is no developer warning raised. Your second proposition is interesting, but it can not change the level of errors raised by CMake to fatal.
    – louisiuol
    Aug 13, 2020 at 18:26
  • AUTHOR_WARNING = CMake Warning (dev), continue processing Jul 6, 2022 at 19:19
  • 2

2 Answers 2


You could try using a tool like awk or sed to parse the CMake output and extract the first error message. Here is an example of how you could use both:

$ cmake . 2>&1 | awk '/^CMake Error/ { print; exit }' // or
$ cmake . 2>&1 | sed -n '/^CMake Error/{p;q;}'

This will print the first error message that CMake outputs and then exit. You could then use this output to help identify the source of the error.


You could use less or ccmake to page through the output, starting at the top. The controls are part of less, and it does not fill up your terminal buffer (it uses a separate buffer). You will need to pipe stderr to stdout, using the redirection 2>&1. (CCMake is a terminal UI, separate package)

For example, cmake .. 2>&1 | less or ccmake .. and then c to run configure

If you want to hide other output, i.e. detection info, you can pipe stdout to null (cmake .. 2>&1 >/dev/null).

To show only warnings/only errors (the whole message text after it), or showing only one, is hard to do, because they can contain an empty blank line for example, and aren't always as neat as those two errors.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .