What is the message() command in CMake?

The message() command is to display messages during the configuration and build process. This feature provides the user or developer with information, debugging details, or updates on the current status of a CMake script while it is running. It facilitates interaction between script creators and users, allowing feedback to be shared, important information to be conveyed, and potential problems to be identified.

Syntax

The basic syntax of the message() command is as follows:

message([<mode>] "message text")

Here the <mode> specifies the mode of the message, and the message text is the text we want to display.

Types of mode
Types of mode

The <mode>

The <mode> parameter is optional and can be one of the following values:

  • STATUS: The message appears as a status message indicating some progress or information about the build.

  • WARNING: The messages appear as warnings and indicate potential problems or areas that may require attention.

  • AUTHOR_WARNING: Similar to WARNING, but applies to script authors rather than script users.

  • SEND_ERROR: The message is considered an error, causing CMake to stop the configuration process.

  • FATAL_ERROR: This message is considered a fatal error, causing CMake to terminate immediately.

  • DEPRECATION: The message is a deprecation warning, indicating that some functionality is being phased out.

Example

Here are some examples of using the message() command. To execute, use the cmake -P msg.cmake command in the following terminal:

message("\nPrint without mode parameter...\n")
message(DEPRECATION "This feature will be removed in the next release.")
message(STATUS "Configuring my project...\n")
message(WARNING "Consider using the new API instead.")
message(SEND_ERROR "Required library not found.")
message(FATAL_ERROR "Cannot continue without necessary dependencies.")
Playground for the message() command

Explanation

The given code uses the message() command in CMake to print different types of messages (normal, deprecation, status, warning, and errors) to the console, each serving a specific purpose in the build and configuration process. Below is a breakdown provided line by line:

Line 1: This line prints a message to the console using the message() command in CMake. The printed message is "Printing without mode parameter...", enclosed in double quotes. The \n character is an escape sequence representing a newline so that the message will be printed on the newline.

Line 2: This line also uses the message() command to print the message but includes a mode parameter, in this case, DEPRECATION.

Line 3: The message() command is used with the mode parameter STATUS to print an informational message.

Line 4: The message() command is used with the WARNING mode to generate a warning message.

Line 5: The message() command with the SEND_ERROR mode. Using this mode would generally cause the CMake configuration to fail.

Line 6: Finally, the message command with the FATAL_ERROR mode is used to produce a fatal error message.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved