4

I am trying to learn CMake from http://www.cmake.org/cmake-tutorial/. I don't follow how set syntax works. For example, from this tutorial,

set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)

I could understand that here we want to assign MathFunctions to the EXTRA_LIBS variable. But what I am confusing is why we want to have EXTRA_LIBS ${EXTRA_LIBS}. Why not just

set (EXTRA_LIBS MathFunctions)

Moreover, I test with following code

set (VALUE_1 "value 1")  # A
set (VALUE_2 ${VALUE_2} "value 2")  # B

message("value 1:" ${VALUE_1})
message("value 2:" ${VALUE_2})

in this case, both # A and # B produce same format of result.

So my question is what is difference between # A and # B?

1
  • 1
    It's so that EXTRA_LIBS will be set to its current value and MathFunctions appended. E.g. if EXTRA_LIBS is foo bar then set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions) expands to set (EXTRA_LIBS foo bar MathFunctions) thus setting EXTRA_LIBS to foo bar MathFunctions. Commented Mar 16, 2015 at 13:59

2 Answers 2

6

So my question is what is difference between # A and # B

In first case #A you assign "value 1" to variable VALUE_1, but in second case you assign to variable VALUE_2 already existed value of this variable which is concatenated with "value 2".

For example:

set (VALUE_1 "default value")
set (VALUE_2 "default value")

set (VALUE_1 "value 1")  # A
set (VALUE_2 ${VALUE_2} "value 2")  # B

message("value 1:" ${VALUE_1})
message("value 2:" ${VALUE_2})

Output:

...
value 1:value 1
value 2:default valuevalue 2
...

Another words the second case is the way to modify already existed variable.

It can be useful when you don't want overwrite existed value, for instance:

MainProject/CMakeLists.txt:

set (CXX_COMPILER_FLAG "some optimization flags")
add_subdirectory (SubProject)

MainProj/SubProject/CMakeLists.txt:

set (CXX_COMPILER_FLAG "some warning flags") #<- wrong, overwrite flags.
set (CXX_COMPILER_FLAG "${CXX_COMPILER_FLAG} some warning flags") #<- correct, safe existed flags and add new flags.
Sign up to request clarification or add additional context in comments.

Comments

0
set (var_a "value a") # just like: var_a = "value a"
set (var_b ${var_b} "value b") # just like: var_b = var_b + "value b"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.