[cmake-developers] Header-only library targets

Brad King brad.king at kitware.com
Wed Jul 15 09:10:39 EDT 2015


On 07/13/2015 01:19 PM, 蓝星灿 wrote:
> However it's time to change this situation right? I think current version of
> interface library is already closed to what header library project wants,
> through maybe original design goal was not going to do so, but I couldn't see
> any scene to use interface library except for header-only library target.

IIRC the current INTERFACE library functionality was designed with
header-only libraries in mind.  They work well from the buildsystem
perspective: one can "link" to a (header-only) INTERFACE library
using target_link_libraries and get the include directories added
to compilation of one's own sources.

What is missing is a notion of the headers (header-only "sources")
that are actually in the library.  This information is needed in
particular for IDE project file generators like VS and Xcode so
they can make the sources available in the IDE for humans to see.
An open question is where to specify this information.  Perhaps
it should simply be in the add_library call:

 add_library(myHeaderLib INTERFACE a.h b.h)

Currently that produces an error:

 CMake Error at CMakeLists.txt:... (add_library):
   add_library INTERFACE library requires no source arguments.

We could consider lifting this restriction for sources that do
not compile (but make it an error to add a compiling source).

Note that INTERFACE libraries by design cannot specify any actual
build rules, so any appearance in an IDE would be attached to a
dummy target.  This would be similar to current add_custom_target
hacks but could be generated automatically and given the same name
as the interface library target.

-Brad



More information about the cmake-developers mailing list