How to have CMake show headers-that are not part of any binary target-in the IDE?

て烟熏妆下的殇ゞ 提交于 2019-11-30 01:24:50

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

You can use the new target_sources command in 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

The limitation of not being able to export targets which have INTERFACE_SOURCES has been lifted for CMake 3.3.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!