如何在CMake中隐藏某些目标?

发布于 2024-09-30 03:52:54 字数 141 浏览 0 评论 0原文

我有一个库,它是作为基于 CMake 的项目的一部分构建的。这个库有很多目标,我不想看到它们出现在我的目标列表中(在 Visual Studio 或 QtCreator 下)。有没有办法让我将此库构建为我的项目构建的一部分(有点像依赖项构建),但看不到该库的可用目标?

I have a library that is built as part of my CMake-based project. This library has many targets and I am not interested in seeing them appearing in my target list (either under Visual Studio or QtCreator). Is there a way for me to have this library built as part of my project build (kindda like a dependency build) but not seeing the available targets of this library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心碎的声音 2024-10-07 03:52:55

我的解决方案是让 CMake 执行构建我需要的依赖项的 shell 命令,而不是调用 add_subdirectory。它并不优雅,但胜过所有这些不需要的目标造成的视觉污染。

My solution to this is to have CMake execute a shell command that builds the dependencies I need instead of calling add_subdirectory. It's not elegant but beats the visual pollution caused by all those unwanted targets.

一张白纸 2024-10-07 03:52:55

您知道 ExternalProject_Add 命令吗?它允许您描述要构建的库的构建(以及下载、修补、安装...)步骤,作为构建的先决条件
你自己的项目。使用此命令会将您的库显示为 make/Visual Studio/QtCreator 中的单个目标。

Do you know the ExternalProject_Add command? It allows you to describe build (and download, patch, install, ...) steps of libraries that you want to build as a prerequisite of
your own project. Using this command will show your library as a single target in make/Visual Studio/QtCreator.

巾帼英雄 2024-10-07 03:52:54

目前,在这些 IDE 中几乎没有明确支持隐藏特定目标。 CMake(尚)不支持显示/隐藏目标选项。但是,有一种方法可以避免在 Visual Studio 中滚动长目标列表;您可以使用 IDE 文件夹对您的目标进行分组。 CMake 通过设置 FOLDER 特定目标上的属性。

首先,通过将此行添加到顶级 CMakeLists.txt 文件来告诉 CMake 启用 IDE 文件夹:

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

然后,通过设置 FOLDER 属性将目标组织到文件夹中。

# Put these targets in the 'HiddenTargets' folder in the IDE.
set_target_properties(SillyLib1 SillyLib2 DontCareLib1 PROPERTIES FOLDER HiddenTargets)
# Put these targets in the 'AppTools' folder in the IDE.
set_target_properties(WriterLib1 ReaderLib1 AwesomeLib42 PROPERTIES FOLDER AppTools)

现在,您的 CMake 目标将放置在 Visual Studio 解决方案资源管理器中的 HiddenTargetsAppTools 文件夹中。您可以折叠包含您不想看到的目标的文件夹,或完全隐藏该文件夹(右键单击该文件夹,然后选择隐藏文件夹)。目标仍然可用(并且将在必要时构建),只是它们在解决方案资源管理器中不可见。您可以通过右键单击解决方案并选择取消隐藏文件夹来取消隐藏隐藏文件夹。


据我所知,Qt Creator 不支持这一点。然而,在 2017 年,他们解决了类似的隐藏 CMake 目标的请求。一般来说,此错误修复(在 Qt Creator 4.4.0 及更高版本中实现)清理了受污染的目标视图(删除了源目录列表),并改进了其隐藏生成的文件隐藏空文件夹功能,使用户可以更好地控制项目列表中显示/隐藏的内容。

There is currently little explicit support for hiding specific targets in these IDEs. CMake does not (yet) support a show/hide targets option. However, there is a way to avoid scrolling through long lists of targets in Visual Studio; you can group your targets with IDEs folders. CMake supports this, by setting the FOLDER property on specific targets.

First, tell CMake to enable IDE folders by adding this line to your top-level CMakeLists.txt file:

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

Then, organize your targets into folders, by setting the FOLDER property.

# Put these targets in the 'HiddenTargets' folder in the IDE.
set_target_properties(SillyLib1 SillyLib2 DontCareLib1 PROPERTIES FOLDER HiddenTargets)
# Put these targets in the 'AppTools' folder in the IDE.
set_target_properties(WriterLib1 ReaderLib1 AwesomeLib42 PROPERTIES FOLDER AppTools)

Now, your CMake targets will be placed in the HiddenTargets and AppTools folders in the Visual Studio Solution Explorer. You can collapse the folders containing targets you don't want to see, or hide the folder altogether (by right-clicking the folder, and selecting Hide Folder). The targets will still be usable (and will be built if necessary), they just won't be visible in the Solution Explorer. You can un-hide hidden folders by right-clicking the solution, and selecting Unhide Folders.


Qt Creator does not support this, to my knowledge. However, in 2017, they addressed a similar request to hide CMake targets. In general, this bug fix (implemented in Qt Creator 4.4.0 and greater) cleaned up the polluted targets view (removed the Source Directory listing), and improved their Hide Generated Files and Hide Empty Folders capabilities, which gave users more control over what is shown/hidden in the project listing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文