问题描述
前言:我仅是在谈论本地编译,而不是在安装项目。这是因为我没有对使用CMake正确安装进行足够的研究,但是如果我的问题直接与安装$ c有关,请及时输入。
PREFACE: I am only talking about compiling locally, not about installing projects. This is because I haven't done enough research on proper install with CMake, though please chime in if my question directly relates to install practices (seems likely).
-
在什么情况下您不不想将所有正在构建的项目库收集到同一目录中?为什么没有人 CACHE CMAKE _ * _ OUTPUT_DIRECTORY 路径?
In what scenarios do you not want to gather all project libraries being built into the same directory? Why doesn't anybody ever CACHE the CMAKE_*_OUTPUT_DIRECTORY paths?
是否需要直接执行 $< CONFIG> 级规范?
Is there ever a need to perform $<CONFIG> level specifications directly?
常规默认设置是 CMAKE_BINARY_DIR , CMAKE_CURRENT_BINARY_DIR 还是 PROJECT_BINARY_DIR ?
Should the general default be to CMAKE_BINARY_DIR, CMAKE_CURRENT_BINARY_DIR, or PROJECT_BINARY_DIR?
1。要缓存还是不缓存?
来自
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
在网络上的许多项目中都可以看到
which you see in many projects across the web.
- 父级 CMakeLists.txt 无法覆盖这些。
- 如果父项目想要/需要更改这些,例如
- A parent CMakeLists.txt cannot override these.
- If a parent project wants to / needs to change these, e.g. to put all in the same folder, it is impossible.
因此建议的修订版将始终为 缓存路径描述 :
So the suggested revision would be to always CACHE PATH "description":
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib CACHE PATH "Where to place compiled static libraries.") set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib CACHE PATH "Where to place compiled shared libraries.") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin CACHE PATH "Where to place compiled executables.")
- 是有什么原因不进行缓存?
- Is there any reason not to cache?
这应该总是 给发电机,是吗?我的理解(和经验)是通过设置非配置工具,使用按配置文件夹创建的生成器(Visual Studio,XCode等)仍然满足于将它们放置在通常情况下的需求。也就是不设置配置级别 CMAKE _ * _ OUTPUT_DIRECTORY _ * ,这就是我将控制权传递给了生成器,让它决定。
This should pretty much always be left to the generator, yes? My understanding (and experience) is by setting the non-config one, generators (Visual Studio, XCode, ...) that use folders per-config build are still content to place things where they ordinarily would. AKA by not setting config level CMAKE_*_OUTPUT_DIRECTORY_* I am passing control to the generator to let it decide.
另一方面,说开发人员不希望他们正在构建的其他项目最终位于同一目录中。我是否应该不再使用 CMAKE_CURRENT_BINARY_DIR 或 PROJECT_BINARY_DIR 吗?如果只使用原始的 CMAKE_BINARY_DIR 而没有 CACHE ,那么我绝对没有办法防止我正在结束的一个子项目。
On the other hand, say a developer does not want other projects they are building to end up in the same directory. Should it not further be the case that I use CMAKE_CURRENT_BINARY_DIR OR PROJECT_BINARY_DIR? In the event that just a raw CMAKE_BINARY_DIR is used, without a CACHE, then I have absolutely no way to prevent a sub-project I am building from ending up next to mine.
通过使用 CMAKE_CURRENT_BINARY_DIR 或 PROJECT_BINARY_DIR ,如果用户不希望该库结束于父项目旁边,则只需设置 CMAKE _ * _ OUTPUT_DIRECTORY 变量配置后。
By using either CMAKE_CURRENT_BINARY_DIR or PROJECT_BINARY_DIR, if a user does not want this library to end up next to the parent project they can simply set the CMAKE_*_OUTPUT_DIRECTORY variables after configuring this one.
基本上,对于如何使用这些变量似乎没有任何标准。我对CMake的多功能性感到很兴奋,我并不是说他们应该在这里进行任何默认设置-这将由项目确定。我试图了解最合适的默认选择是什么,这也使开发人员可以选择忽略我的默认设置。
Basically, there don't seem to be any standards for how these variables ought to be used. I'm thrilled at how versatile CMake is, I'm not saying they should be doing any defaults here at all -- that is to be determined by the project. I'm trying to understand what the most appropriate default choice is, that also allows developers to bypass my defaults if they so choose.
推荐答案
我在@Tsyvarev。我没有看到启用CMake的项目的用户想要更改构建工件的输出路径,而只是更改安装路径。
I'm with @Tsyvarev. I don't see the user of a CMake enabled project wanting to change the output path of the build artifacts, only the install path.
仅不要将 CMAKE _ * _ OUTPUT_DIRECTORY 变量设置为已缓存,因为如果另一个CMake项目使用您的子项目作为子项目,在某些情况下,您将覆盖父项目的设置(缓存的变量是全局的)。
Just don't set the CMAKE_*_OUTPUT_DIRECTORY variables as cached, because if another CMake project uses yours as a sub-project you would under certain circumstances overwrite your parent project's settings (cached variables are global).
是。仅当生成器的默认值不适合( Config Name = Sub Folder Name)时才提供。
Yes. It's only given when the generator's defaults are not fitting ("Config Name" = "Sub Folder Name").
所以我想说保存变量将是先用 if()语句检查是否有人设置了变量并使用:
So I would say the save variant would be to first check with an if () statement if someone else has already set the variable and also to use of PROJECT_BINARY_DIR:
if (NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib") endif()
然后,您项目的用户仍可以从外部项目或父项目可以设置它,如果未设置,则具有默认值。
Then a user of your project may still set the variables from the outside or a parent project could set it and you have a default if it was not set.
这篇关于正确使用CMAKE _ * _ OUTPUT_DIRECTORY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!