简体   繁体   English

cmake变量和cmake参数之间的区别

[英]Difference between cmake variables and cmake arguments

I have been given some source code, along with a CMakeLists.txt file, and told to run cmake by: 我已经获得了一些源代码,以及一个CMakeLists.txt文件,并告诉我运行cmake:

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. 我还注意到有一个名为FindOpenNI.cmake的文件,我相信在cmake调用find_package(OpenNI)时会使用它。

Therefore, I am guessing that OPEN_NI_ROOT is some kind of variable that is used by cmake for the remainder of setup. 因此,我猜测OPEN_NI_ROOT是cmake用于设置其余部分的某种变量。

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. 但是,我尝试将行set(OPEN_NI_ROOT "/home/karnivaurus/OpenNI")插入到我的CMakeLists.txt文件中,以避免在命令行中将其添加为参数。 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 FindOpenNI.cmake文件是开源的,可以在以下网址找到: https//github.com/victorprad/InfiniTAM/blob/master/InfiniTAM/cmake/FindOpenNI.cmake

The issue is this line in FindOpenNI.cmake ( link ): 问题是FindOpenNI.cmakelink )中的这一行:

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. 这将设置OPEN_NI_ROOT除非它已经在缓存中。 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. 不在缓存中设置变量,因此当命中FindOpenNI.cmake中的FindOpenNI.cmake它将被覆盖。 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 : 避免必须设置命令行选项的最简单方法是在您自己的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: 在这种情况下,要么从干净的构建目录工作,要么设置FORCE选项:

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 . 您可以在此处找到有关此机制的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM