开发者

How to hide certain targets in CMake?

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 Vis开发者_Python百科ual 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?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜