xcode 如何让 CMake 在 IDE 中显示不属于任何二进制目标的头文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27039019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to have CMake show headers-that are not part of any binary target-in the IDE?
提问by Ad N
In our workflow, we can have a module Athat is composed of several header files, module Anot producing any binary (side note: it will obviously be used by other modules, that include some of the headers from module Ato produce binaries).
在我们的工作流程,我们可以有一个模块,一个是由几个头文件,模块A不产生任何二进制(旁注:这显然会被其他模块,包括一些来自标题中使用模块A,产生的二进制文件) .
A good example would be a header-only library, for which CMake 3 introduces a good support thanks to the notion of INTERFACE
library (see this SO answer, and CMake's documentation of the feature).
一个很好的例子是只有头文件的库,由于INTERFACE
库的概念,CMake 3 为它引入了很好的支持(参见这个 SO 答案和 CMake 的特性文档)。
We can make an interface library target out of module A:
我们可以从模块 A 中创建一个接口库目标:
add_library(module_A INTERFACE)
That gives us all the nice features of CMakes targets (it is possible to use it as another target's dependency, to export it, to transitively forward requirements etc.)
这为我们提供了 CMakes 目标的所有优秀功能(可以将其用作另一个目标的依赖项、导出它、传递性转发需求等)
But in this case, the headers in module Ado not show up in our IDE (Xcode, yet we expect it to be the same with most/every other IDE).
但在这种情况下,模块 A 中的头文件不会出现在我们的 IDE 中(Xcode,但我们希望它与大多数/所有其他 IDE 相同)。
This proves to be a major drawback in the workflow, since we need the files composing module Ato be shown in the IDE for edition. Is it possible to achieve that ?
这被证明是工作流中的一个主要缺点,因为我们需要在 IDE 中显示组成模块 A的文件以进行编辑。有可能实现吗?
回答by Ad N
Several months down the line, I did not find a way to directly list the header files for an INTERFACE
library.
几个月后,我没有找到直接列出INTERFACE
库头文件的方法。
Since the question still has some views, here is what I ended up doing (i.e. what appears like the lesser hack currently available).
由于这个问题仍然有一些观点,这就是我最终做的事情(即看起来像目前可用的较小的黑客)。
Imagine module Ais a header only library. In the CMakeLists.txt declaring its target:
想象一下,模块 A是一个只有头文件的库。在声明其目标的 CMakeLists.txt 中:
# Define 'modA_headers' variable to list all the header files
set(modA_headers
utility.h
moreUtilities.h
...)
add_library(moduleA INTERFACE) # 'moduleA' is an INTERFACE pseudo target
#
# From here, the target 'moduleA' can be customised
#
target_include_directories(moduleA ...) # Transitively forwarded
install(TARGETS moduleA ...)
#
# HACK: have the files showing in the IDE, under the name 'moduleA_ide'
#
add_custom_target(moduleA_ide SOURCES ${modA_headers})
I do not accept this answer, since I expect further releases of CMake to offer a more semantically correct approach, which will then be accepted : )
我不接受这个答案,因为我希望 CMake 的进一步版本提供更语义正确的方法,然后将被接受:)
回答by steveire
You can use the new target_sources
command in CMake 3.1.
您可以target_sources
在 CMake 3.1 中使用新命令。
add_library(moduleA INTERFACE)
target_include_directories(moduleA INTERFACE ...)
target_sources(moduleA INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/utility.h
${CMAKE_CURRENT_SOURCE_DIR}/moreUtilities.h
)
It is also transitive.
它也是可传递的。
http://www.cmake.org/cmake/help/v3.1/command/target_sources.html#command:target_sources
http://www.cmake.org/cmake/help/v3.1/command/target_sources.html#command:target_sources
The limitation of not being able to export targets which have INTERFACE_SOURCES has been lifted for CMake 3.3.
CMake 3.3 取消了无法导出具有 INTERFACE_SOURCES 的目标的限制。