Issue
I'm relatively new to CMake, right now I experiment with it on a Windows machine with WSL (I run CMake in WSL), and I have confusion over the way CMake handles variables.
My issue with the variables is that sometimes I feel I meet variables, that, according to how (I think) CMakeLists.txt is parsed, contain some values, but I never set any value to them yet they contain values, and I have no idea what other variables with set values exist.
I failed to turn up any information that would make it clear once and for all. Here is a specific example, from my own CMakeLists.txt. I decided I want to have Boost as a dependency:
find_package(Boost 1.74.0 COMPONENTS filesystem system REQUIRED)
message(${Boost_LIBRARIES})
target_link_libraries(app_release PUBLIC
${Boost_LIBRARIES}
)
And the message() (which I added as a debugging output) output is Boost::filesystemBoost::system
. This CMakeLists.txt works correctly without errors, I can make-compile a hello world program with it.
I never set the variable Boost_LIBRARIES. The only reason I have it in my CMakeLists.txt is because I copied this piece of CMakeLists from another place, otherwise I would have never known that such a variable exists. I also failed to turn up anything that would hint at existence of such a variable in the documentation (CMake documentation seems to contain a lot of gaps?)
Source of "minimal complete" CMakeLists.txt example file I pulled it from StackOverflow, I use it as "inspiration" for writing my CMakeLists.txt.
Questions:
- How am I supposed to know that the variable Boost_LIBRARIES exists? There has to be some logic to how they're created, I would think. My random guess is that _LIBRARIES is appended to package name I have just find_package()'d, but I couldn't find anything in the docs about it.
- Can I get a list of all hidden variables and their values then?
Solution
How am I supposed to know that the variable Boost_LIBRARIES exists?
From documentation of a specific package. In this case, https://cmake.org/cmake/help/latest/module/FindBoost.html .
There has to be some logic to how they're created, I would think. My random guess is that _LIBRARIES is appended to package name I have just find_package()'d, but I couldn't find anything in the docs about it
In the beginning, there was chaos. Basically find_package
all it does it executes a CMake script, that can do anything. Every package set different variables. Nowadays, it's a bit more standardized https://cmake.org/cmake/help/book/mastering-cmake/chapter/Finding%20Packages.html#built-in-find-modules , but it still depends on specific script on what it actually does.
Can I get a list of all hidden variables and their values then?
There are no "hidden" variables in CMake. All variables are normal. There are cached and non-cached variables.
After generating CMake build, you can look at all cache variables <builddir>/CMakeCache.txt
.
To print all variables, see CMake: Print out all accessible variables in a script .
Answered By - KamilCuk Answer Checked By - Candace Johnson (WPSolving Volunteer)