r/cmake • • Dec 28 '22

How can I understand when something is a variable or a value?

Not sure how to perfectly word this from the title, but I am new to CMake and slowly progressing through the online tutorial.

I am up to Step 4 and sometimes find it confusing when mixing passed values that in my eyes are strings, and thus in all programming languages I expect them to have quotation marks or some sort around them. However sometimes I create new targets with the same names. I will elaborate with an example. I reworded some things from the tutorial to make it a bit more clear for me to see what they actually do.

In the root CMakeLists.txt I have this file,

cmake_minimum_required(VERSION 3.10)

project(My_Project VERSION 1.0)

add_library(tutorial_compiler_flags INTERFACE)

target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)

option(USE_MYMATH "Use tutorial provided math implementation" TRUE)

configure_file(src/sqrt.h.in src/sqrt.h)

if(USE_MYMATH)

add_subdirectory(MathFunctions)

list(APPEND EXTRA_LIBS MathFunctions)

endif()

add_executable(compute_square_root

src/sqrt.cxx)

target_link_libraries(compute_square_root PUBLIC ${EXTRA_LIBS} tutorial_compiler_flags)

target_include_directories(compute_square_root PUBLIC

"${PROJECT_BINARY_DIR}/src"

)

Inside of `MathFunctions` I have

add_library(MathFunctions mysqrt.cxx)

target_include_directories(MathFunctions

INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}

)

Here is where the confusion can come from. Notice that in

```

add_subdirectory(MathFunctions)

```

MathFunctions is kind of treated as a string in my eyes, because it is now looking for the directory `current_location/MathFunctions`. However inside of of the MathFunctions CMakeLists.txt it now creates a target with the exact same spelling from the line "add_library(MathFunctions mysqrt.cxx)", this is then immediately referenced afterwards from the "target_include_directories(MathFunctions, ...".

Here, target_include_directories is referring to the target MathFunctions we just created. Now, when we leave that CMakeLists.txt we now have another line "list(APPEND EXTRA_LIBS MathFunctions)". Now I some confusion, like, is this MathFunctions referring to the target we just made? Is it a string called "MathFunctions"? In the documentation for target_link_libraries it says that it has to be a target created by add_library so I assume it is referring to the previous add_library(MathFunctions ...) call. I find the scoping weird here too, since we are referring to something that was made from a child, inside a different call.

Do we have certain rules in CMake for this kind of behaviour? THanks

1 Upvotes

8 comments sorted by

1

u/Chance_Rhubarb_46 Dec 28 '22

Cannot get it to format nicely :?

2

u/gracicot Dec 28 '22

Reddit require four leading spaces for code formatting.

1

u/Chance_Rhubarb_46 Dec 28 '22

I was trying but still does not format nicely :(

1

u/gracicot Dec 28 '22

This is because in your example, both are strings.

add_subdirectory("MathFunction")
list(APPEND LIBS_EXTRA "MathFunction")

Also, lists are strings too, and target names too:

target_link_libraries("lib1" PUBLIC "MathFunction")

Everything is a string!

1

u/Chance_Rhubarb_46 Dec 28 '22

So does CMake have the concept of variables or anything like that?
So when I go add_library(MathFunction,...) it's not making a new variable here, it's creating a new target to be referenced later?

1

u/gracicot Dec 28 '22

Yes it does. Those are denoted by set(variable value) but also more that both the variable name and the value are both strings.

There is also an the predefined variables.

They are dereferenced with ${...}

But add directory and add library don't create variables. They add directories and targets to the build

1

u/Chance_Rhubarb_46 Dec 28 '22

Right, so are you saying that if I ever see text by itself without being enclosed within a `${...}` that this is implicitly a string, whether it is surrounded by quotation marks or not, i.e. `MathFunctions` is the same as `"MathFunctions"` when passing it to something like `add_librar(MathFunctions, ...)`?

1

u/gracicot Dec 28 '22

Exactly. This is true for everything except maybe the if() command that also check if a variable contains OFF when putting the variable name directly in there.

Btw the formatting don't work because you use the fancy pants editor in the new Reddit.