如何使用 cmake 为嵌套子目录设置 Visual Studio 过滤器
问题描述
我有以下结构
Main/CMakeLists.txt
Main/Dir/CmakeLists.txt
它在 Visual Studio 中生成了以下结构
It generated the following structure in Visual Studio
我想要的:
我的尝试:
Main/CMakeLists.txt
Main/Dir/CmakeLists.txt
我得到了什么:
请帮我解决这个问题.
- 我不想在带有过滤器的主目录中使用单个 CmakeFile.txt
- 实际结构是多层深嵌套结构.所以请提出适用于任何级别子目录的解决方案
推荐答案
有几个现成的或适应性强的解决方案来模拟源树行为,例如在 Eclipse 中使用 CMake for Visual Studio(例如 ADD_SRC_SUBFOLDER DESTINATION_SRCS
来自 Zobra 或 GroupSources 来自 Luca).
There are several ready to use or adaptable solutions out there to mimic a Source Tree behavior like in Eclipse with CMake for Visual Studio (e.g. ADD_SRC_SUBFOLDER DESTINATION_SRCS
from Zobra or GroupSources from Luca).
这是我针对您的用例的简化版本:
Here is my reduced version for your use case:
参见source_group()的文档
你必须用双反斜杠给子目录.
See the documentation of source_group()
that you have to give the sub-directories with double backslashes.
出于为什么我将您的 file(GLOB ...)
替换为我喜欢引用 CMake 的 file()
命令文档:
For the reason why I replaced your file(GLOB ...)
with a dedicated list of all source files I like to quote from CMake's file()
command documentation:
我们不建议使用 GLOB
从以下位置收集源文件列表你的源代码树.如果在源文件中没有 CMakeLists.txt 文件更改添加或删除然后生成的构建系统不知道何时要求 CMake 重新生成.
We do not recommend using
GLOB
to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.
这是我用作函数的故障安全版本(检查绝对路径):
And here is my fail-safe version (that checks for absolute paths) to be used as a function:
您将在示例中使用
这篇关于如何使用 cmake 为嵌套子目录设置 Visual Studio 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!