Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between cmake variables and cmake arguments

Tags:

c++

ubuntu

cmake

I have been given some source code, along with a CMakeLists.txt file, and told to run cmake by:

cmake ../src -DOPEN_NI_ROOT=/home/karnivaurus/OpenNI

I have also noticed that there is a file called FindOpenNI.cmake, which I believe is used when find_package(OpenNI) is called by cmake.

Therefore, I am guessing that OPEN_NI_ROOT is some kind of variable that is used by cmake for the remainder of setup.

However, I have tried inserting the line set(OPEN_NI_ROOT "/home/karnivaurus/OpenNI") into my CMakeLists.txt file, in an attempt to avoid the need to add it as an argument at the command line. But this does not seem to do the same thing.

Can somebody please explain how these two variable types are different?


The file FindOpenNI.cmake is open source and can be found at: https://github.com/victorprad/InfiniTAM/blob/master/InfiniTAM/cmake/FindOpenNI.cmake

like image 575
Karnivaurus Avatar asked Aug 21 '15 21:08

Karnivaurus


People also ask

How do you use variables in CMake?

You access a variable by using ${} , such as ${MY_VARIABLE} . CMake has the concept of scope; you can access the value of the variable after you set it as long as you are in the same scope. If you leave a function or a file in a sub directory, the variable will no longer be defined.

Where are CMake variables stored?

Cache variables are stored in the CMake cache file, and are persisted across CMake runs.

How do I declare a variable in CMake?

Options and variables are defined on the CMake command line like this: $ cmake -DVARIABLE=value path/to/source You can set a variable after the initial `CMake` invocation to change its value. You can also undefine a variable: $ cmake -UVARIABLE path/to/source Variables are stored in the `CMake` cache.

What is CMake cache variable?

The CMake cache may be thought of as a configuration file. The first time CMake is run on a project, it produces a CMakeCache. txt file in the top directory of the build tree. CMake uses this file to store a set of global cache variables, whose values persist across multiple runs within a project build tree.


1 Answers

The issue is this line in FindOpenNI.cmake (link):

set(OPEN_NI_ROOT "/usr/local" CACHE FILEPATH "Root directory of OpenNI2")

This will set OPEN_NI_ROOT unless it's already in the cache. A simple call to:

set(OPEN_NI_ROOT "/home/karnivaurus/OpenNI")

does not set the variable in the cache, so it will be overridden when the line in FindOpenNI.cmake is hit. Using the command line form of setting the variable will set it in the cache, which is why it works just fine.

The easiest way to avoid having to set the command line option is to set the cache explicitly in your own CMakeLists.txt:

set(OPEN_NI_ROOT "/home/karnivaurus/OpenNI" CACHE FILEPATH "Root directory of OpenNI2")

If you're working from a dirty build directory, it's likely this cache variable already exists, so this line would have no effect. In that case, either work from a clean build directory, or set the FORCE option:

set(OPEN_NI_ROOT "/home/karnivaurus/OpenNI" CACHE FILEPATH "Root directory of OpenNI2" FORCE)

This will write over the cached value. Note that this would prevent you from setting the option in the command line in the future, which is why this method isn't preferred. You can find some more information about the mechanics of this here.

like image 180
huu Avatar answered Sep 30 '22 17:09

huu