如何在IDE中有CMake显示标题,不是任何二进制目标的一部分? [英] How to have CMake show headers-that are not part of any binary target-in the IDE?

查看:141
本文介绍了如何在IDE中有CMake显示标题,不是任何二进制目标的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的工作流程中,我们可以有一个由多个头文件组成的模块A 模块A 不生成任何二进制文件通过其他模块,包括来自模块A 的一些头文件来生成二进制文件)。



一个很好的例子是一个只有头文件的库,由于 INTERFACE 库的概念,CMake 3引入了良好的支持(参见此SO回答和CMake的文档

我们可以从模块A 中创建一个接口库目标:

  add_library(module_A INTERFACE)

这给了我们CMakes目标的所有好的功能(可以使用它作为另一个目标的依赖,导出它,过渡性地转发需求等)。



但是在这种情况下,模块A 中的标头不会显示在我们的IDE(Xcode,但我们希望它与大多数/每个其他IDE相同)。



这证明是工作流程中的一个主要缺点,因为我们需要组成模块A 的文件在IDE中显示版本。是否可以实现这一点?

解决方案

几个月下来,我没有找到一种方法直接列出头文件对于 INTERFACE 库。



由于问题仍然有一些意见,这里是我最后做的



想象模块A 是一个只有标题的库。在CMakeLists.txt中声明它的目标:

 #定义modA_headers变量来列出所有的头文件
set (modA_headers
utility.h
moreUtilities.h
...)

add_library(moduleA INTERFACE)#'moduleA'是INTERFACE伪目标


#从这里,目标'moduleA'可以定制

target_include_directories(moduleA ...)#传递转发
install(TARGETS moduleA .. 。)


#HACK:将文件显示在IDE中,名称为moduleA_ide

add_custom_target(moduleA_ide SOURCES $ {modA_headers})

我不接受这个答案,因为我期望CMake的更多版本提供更语义正确的方法,然后将被接受:)


In our workflow, we can have a module A that is composed of several header files, module A not producing any binary (side note: it will obviously be used by other modules, that include some of the headers from module A to produce binaries).

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).

We can make an interface library target out of module 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.)

But in this case, the headers in module A do not show up in our IDE (Xcode, yet we expect it to be the same with most/every other IDE).

This proves to be a major drawback in the workflow, since we need the files composing module A to be shown in the IDE for edition. Is it possible to achieve that ?

解决方案

Several months down the line, I did not find a way to directly list the header files for an INTERFACE library.

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 A is a header only library. In the CMakeLists.txt declaring its target:

# 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 : )

这篇关于如何在IDE中有CMake显示标题,不是任何二进制目标的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆