Skip to content

Print a message

Sometimes you just want to print a message. This can be useful for debugging or notifying the user of a status. Another good use case is printing out a help-like menu of available -DMYOPTION=myvalue flags if your CMake compilation is complex.

The message() function prints the given message at generation-time (cmake -B build). If you want to print a message at build-time (cmake --build build), you probably want something like #pragma message(), #warning, or #error.

⚠️ This example will not generate successfully or compile sucessfully due to the message(FATAL_ERROR) and #error errors respectively.

c
#pragma message("Hello from main.c")
#warning "Hmmm... Using cute dog images instead."
#error "Really bad! Not running on KittenOS!"
#include <stdio.h>
int main() {
  puts("Hello world!");
  return 0;
}
cmake
cmake_minimum_required(VERSION 3.28)
project(my-project)
add_executable(my-app main.c)
message("Hello world!")
message(WARNING "Using fallback data...")
message(FATAL_ERROR "Oh no! Not enough cat images!")
sh
# 🛑 These fail given the 👆 example code
cmake -B build
cmake --build build
./build/my-app

There are other enum-like values that can be used in the message() function:

(none)         = Important information
STATUS         = Incidental information
WARNING        = CMake Warning, continue processing
AUTHOR_WARNING = CMake Warning (dev), continue processing
SEND_ERROR     = CMake Error, continue processing,
                              but skip generation
FATAL_ERROR    = CMake Error, stop processing and generation
DEPRECATION    = CMake Deprecation Error or Warning if variable
                 CMAKE_ERROR_DEPRECATED or CMAKE_WARN_DEPRECATED
                 is enabled, respectively, else no message.

📚 Further reading: message | CMake