How to Print Without Newline In Cmake?

2 minutes read

In CMake, the message command is used to print output to the console. By default, each message is printed on a new line. However, if you want to print multiple messages without adding a newline, you can use the message command with the UNIX option.


For example:

1
2
3
message("This is ")
message("printed on ")
message(UNIX "the same line.")


When you run this code, the output will be:

1
This is printed on the same line.


By using the UNIX option, you can print multiple messages without adding a newline in CMake.


What is the purpose of the message command in CMake?

The purpose of the message command in CMake is to print a message to the console during the configuration process. This can be useful for displaying information or status updates to the user, debugging purposes, or providing general feedback during the build process. The message command takes a string argument that will be output to the console when the CMake script is executed.


How to suppress newlines in the output of the message command in CMake scripts?

To suppress newlines in the output of the message command in CMake scripts, you can use the MESSAGE command with the APPEND or NOLINE option.

  1. Using the APPEND option:
1
MESSAGE(APPEND "Your message here")


  1. Using the NOLINE option:
1
MESSAGE(NOLINE "Your message here")


Using either of these options will allow you to output text without a newline character at the end. This can be useful when you want to output multiple messages on the same line or control the formatting of your output in CMake scripts.


How to print system information without newline in CMake?

In CMake, you can use the message command to print system information without a newline. Here's an example:

1
2
3
4
5
# Print system information without newline
message("- Number of processors: " ${CMAKE_NUMBER_OF_LOGICAL_PROCESSORS})

# Print a message with a newline
message("\nThis is a new message with a newline")


This will output:

1
2
- Number of processors: 8
This is a new message with a newline


By default, the message command adds a newline at the end of the printed message. If you want to print information without a newline, you can concatenate the information directly to the message string.

Facebook Twitter LinkedIn Telegram

Related Posts:

In CMake, the dollar sign ($) is a special character used to escape variables and functions. If you need to include a literal dollar sign in a string, you can escape it by using double dollar signs ($$) or enclosing it in double quotes ("$"). This will...
To unset a variable in CMake using the command line, you can use the -U flag followed by the variable name. For example, to unset a variable named MY_VARIABLE, you can use the command cmake -UMY_VARIABLE. This will remove the variable from the CMake cache, ess...
When using CMake, you can find the compiler path automatically by specifying the CMAKE_C_COMPILER and CMAKE_CXX_COMPILER variables in your CMakeLists.txt file. CMake will automatically detect the compiler based on your system configuration. Additionally, you c...
In CMake, a macro is a way to define reusable blocks of CMake code. You can define a macro using the macro() command, followed by the name of the macro and the code block that defines its behavior.To use a macro in CMake, you simply invoke the macro by using t...
In CMake, variables are defined using the set() command. To define a variable during build with CMake, simply use the set() command followed by the variable name and its value. For example, to define a variable called MY_VARIABLE with the value Hello World, yo...