使用-CMake中的-D标志和使用set()有什么区别?

huangapple go评论73阅读模式
英文:

What's the difference between using the -D flag and using set() in CMake?

问题

与使用命令行和 -D 标志来设置变量不同,我一直在我的CMakeLists.txt文件中使用set()来为我的简单“学习者”单目录项目设置变量。优点是每次调用cmake -DVARIABLE_NAME=path/to/infinity/and/beyond ..时不必记住和/或复制粘贴长目录名称。

我的问题是这两种方法之间的功能差异是什么,有一些简单的用例可能我不想使用set而是使用命令行的-D标志?

相关问题

这个答案讨论了CMake中变量的作用域,并可能包含了我问题的答案,但对我来说太复杂了,我无法理解:https://stackoverflow.com/questions/31037882/whats-the-cmake-syntax-to-set-and-use-variables

这个答案提供了一个用例,其中这两种方法不相同,但是针对一些我不了解的特定变量和库,因此无法参考:https://stackoverflow.com/questions/63268761/setting-d-variables-inside-cmake

**注意:**我的问题没有特定的上下文,但受到了以下pytorch教程的启发,以设置我的第一个C++项目并使用pytorch。

英文:

Rather than using the command line and the -D flag to set variables, I've been using set() inside the CMakeLists.txt for my simple "learner" one-directory project. The advantage is not having to remember and/or copy and paste long directory names every time I call cmake -DVARIABLE_NAME=path/to/infinity/and/beyond ..

My question is what is the functional difference between the two methods, and what are some simple use cases where I might not want to use set instead of the command line -D flag?

This answer talks about scope of variables in cmake, and may contain the answer to my question, but it is too convoluted for me to follow: https://stackoverflow.com/questions/31037882/whats-the-cmake-syntax-to-set-and-use-variables

This answer provides a use case where the two methods are not the same, but is for some specific variable and library I have no knowledge of so can't relate to: https://stackoverflow.com/questions/63268761/setting-d-variables-inside-cmake

Note: My question does not have a specific context but was inspired by following this pytorch tutorial to setup my first C++ project and use pytorch.

答案1

得分: 2

-D 设置变量为 缓存变量 (另请参阅掌握 CMake 章节变量文档),这意味着即使它们没有在配置文件中设置,它们也会被记住。您可以选择指定类型。如果将类型设置为 PATHFILEPATH,则该值将被转换为绝对路径。

set 设置变量。您可以传递 CACHE 以将变量设置为缓存变量。您还可以使用 TYPE 参数设置类型。非缓存变量的范围为块、函数和目录范围 (文档)。您可以指定类型,还可以指定文档字符串。

缓存变量和非缓存变量都可以使用相同的语法引用 (文档),并相关的细微差别:策略 CMP0126

一般来说,如果变量是您认为构建项目的其他人可能想要控制的东西,那么您应该让用户使用 -D,并在您的 CMakeLists.txt 中使用 set(... CACHE ...) 设置一个“默认值”。请注意,对于潜在的非缓存变量,您可以使用 if(DEFINED ...) 条件性地只在变量尚未设置时设置变量。我在我的回答中也写了关于这个问题的内容:“CMakePresets.json vs CMakeSettings.json vs CMakeLists.txt”。如果变量是布尔值,有一个用于定义缓存变量的速记命令:option

优点是不必每次调用 cmake 时记住和/或复制粘贴长目录名称...

请注意,大多数 shell 都具有历史功能,因此您可以回顾过去执行的命令。实际上,CMake 的预设功能似乎非常适合您。您可以创建一个适合您个人需求的用户本地 CMakeUserPresets.json 文件(将其放入您的 .gitignore 中)。请注意,如果您正在调用 CMake 重新配置和生成已配置和生成的构建系统,以更改缓存变量,那么还有一些工具可以为您简化这个过程,包括捆绑的交互式对话框cmake-guiccmake

英文:

-D sets variables as cache variables (see also Mastering CMake chapter, and variables docs), which means that even though they aren't set in the configuration files, they will be remembered. You can optionally specify a type. If you set the type to PATH or FILEPATH, the value will be converted to an absolute path.

set sets variables. You can pass CACHE to set the variable as a cache variable. You can also set the type using the TYPE argument. Non-cache variables are scoped to blocks, functions, and directory scopes (docs). You can specify a type, and also a docstring.

Both cache and non-cache variables can be referenced using the same syntax (docs and related nuance: policy CMP0126).

Generally speaking, if the variable is something that you think someone building your project might want to have control over, then you should leave it to the user to use -D, and set a "default value" in your CMakeLists.txt by using set(... CACHE ...). Note that for potentially non-cache variables, you can do similar using if(DEFINED ...) to conditionally only set the variable hasn't been set yet. I've written about this also in my answer to "CMakePresets.json vs CMakeSettings.json vs CMakeLists.txt". If the variable is a boolean, there's a shorthand command for defining a cache variable: option.

> The advantage is not having to remember and/or copy and paste long directory names every time I call cmake [...]

Note that most shells have history functionality, so you can recall past commands you've executed. Actually, CMake's Preset feature sounds right up your alley. You can create a user-local CMakeUserPresets.json file (put it in your .gitignore) suited exactly for your personal needs. Note that if you are calling CMake to reconfigure and regenerate an already configured and generated buildsystem for the purposes of changing cache variables, there are several tools which can make that easier for you as well, including the bundled interactive dialogs, cmake-gui and ccmake.

huangapple
  • 本文由 发表于 2023年6月6日 02:27:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定