In CMake, the term string()
command generally refers to a specific type of command used to manipulate and process strings (sequences of characters). These commands enable developers to perform various operations on text, such as concatenation, extraction, substitution, and comparison. Using string()
commands in CMake scripts, users can efficiently handle textual data and achieve more complex configurations and tasks during the build process.
The string()
command is used for concatenating strings. Within the string()
command, we can use the CONCAT
sub-command.
Here’s the syntax for string concatenation in CMake:
string(CONCAT result_string string1 string2 ...)
In the above syntax:
result_string
: The variable that will store the concatenated result.
string1
, string2
: The strings we want to concatenate.
The string()
command allows us to extract substrings from a given input string based on specified indexes or lengths. We can use the SUBSTRING
sub-command. The syntax for extracting a substring using the string()
command is as follows:
string(SUBSTRING <input_string> <start_index> <length> <output_variable>)
In the above syntax:
<input_string>
: The input string from which the substring will be extracted.
<start_index>
: The starting index position from where the extraction should begin. Indexing in CMake is zero-based, meaning the first character is at index 0, the second at index 1, and so on.
<length>
: The number of characters to extract from the input string.
<output_variable>
: The CMake variable where the extracted substring will be stored.
The string()
command allows us to perform string substitutions, which means replacing occurrences of a specific substring within a string with another substring. We can use the REPLACE
sub-command. The syntax for using the string()
command for string substitution is as follows:
STRING(REPLACE <old_substring> <new_substring> <variable_name> <input_string>)
In the above syntax:
<old_substring>
: The substring we want to find and replace in <input_string>
.
<new_substring>
: The substring that will replace each occurrence of <old_substring>
in <input_string>
.
<variable_name>
: The name of the variable where the result of the substitution will be stored.
<input_string>
: The original string that we want to perform the substitution on.
The string()
command allows for various string comparison operations. It is used to manipulate and analyze strings, including performing comparisons. The basic syntax of the string()
command is as follows:
string(COMPARE <OPERATION> <result_variable> <input_string> <input_string>)
In the above syntax:
<result_string>
: The variable that will store the concatenated result.
<input_string>
: The input string from which we compare another input string.
<OPERATION>
: The argument that specifies the type of string operation we want to perform. For string comparison, the relevant operations are typically:
EQUAL
or STREQUAL
: Checks if two strings are equal.
NOTEQUAL
or STRNEQUAL
: Checks if two strings are not equal.
LESS
: Compares two strings lexicographically to see if the first is less than the second.
GREATER
: Compares two strings lexicographically to see if the first is greater than the second.
LESS_EQUAL
: Compares two strings lexicographically to see if the first is less than or equal to the second.
GREATER_EQUAL
: Compares two strings lexicographically to see if the first is greater than or equal to the second.
Here’s a simple program showing some of these actions by executing the cmake -P string.cmake
command in the terminal:
set(string1 "Hello, ") set(string2 "world!") string(CONCAT result_string "${string1}" "${string2}") message("Concatenated string: ${result_string}") string(SUBSTRING "${result_string}" 7 5 extracted_substring) message("Extracted Substring: ${extracted_substring}") string(REPLACE "world!" "Educative!" new_result_string "${result_string}") message("Substitution string: ${new_result_string}") string(COMPARE EQUAL result "${new_result_string}" "${result_string}") if (result) message("The strings are equal.") else() message("The strings are not equal.") endif()
Free Resources