## Nesting Expansion

Revisting the first version of the example of variables, there is something important to consider. Given the original example:  SET( MyVariable AValue ) SET( AnotherVariable MyVariable ) MESSAGE( "MyVariable" ) MESSAGE( "${AnotherVariable}" )  Let's modify it a bit:  SET( MyVariable AValue ) SET( AnotherVariable MyVariable ) MESSAGE( "${MyVariable}" ) MESSAGE( "${${AnotherVariable}}" )  You might be surprised to see the output as:  AValue AValue  What the code in the second 'SET' and 'MESSAGE' lines means now is something quite a bit different. The 'SET' portion has not changed but the way we expand the variable within the 'MESSAGE' text string has. Basically we are saying to expand the variable 'AnotherVariable' and then expand the result of that expansion again. In other words, it is a pointer to another variable in a C like language. Another variation would be:  SET( Target Something ) SET( ${Target}_Name MyLibrary ) SET(${Target}_Type SharedLibrary )  If you guessed that the last two 'SET' commands will make variables named: 'Something_Name' and 'Something_Type', you get a gold star. This makes it possible to create the equivelant of structures within CMake. As much as these are clever ways to use the expansion system, I try to avoid them as overly complicated for what should be a simple secondary portion of your project. There are times and places to use them, hence the description, but try to avoid them for your personal sanity and that of others that have to figure out what the expansions mean.

## Appending To Variables

A common pattern required in make files is to concatenate and append various things together. The set command is used for this with a slightly modified syntax:  SET( Target Part1 ) SET( Target "${Target} Part2" ) MESSAGE( "${Target}" )  Which outputs:  Part1 Part2  Make sure to use the quotes unless you want CMake to eat white space and turn the item into a list.

# Conclusion

In this part we have covered some of the basic commands available in CMake files, how to fix per OS issues and finally how to start building modular projects using static libraries. In the next part we will cover shared libraries and finally begin including the open source unit testing libraries.

Report Article

## User Feedback

Hi AllEightUp.

Just quick suggestion. In section when you introduce "INCLUDE" command it would be good if you could mention to update CMakeProject/CMakeLists.txt file and replace:

with

INCLUDE ( libraries/CMakeLists.txt )

;]

## Create an account

Register a new account

• 0
• 0
• 1
• 1
• 3

• 21
• 13
• 10
• 9
• 18