Discussion:
[cmake-developers] Header-only library targets
Patrik Huber
2015-07-09 20:39:14 UTC
Permalink
Hi all,

I've recently switched from CMake 2.8.12 to 3.2, full of enthusiasm that
header-only libraries would now be better supported through the INTERFACE
option/target. However, the support for header-only libraries isn't really
great, in particular if you're using an IDE like Visual Studio.

The main problem is that if you define an INTERFACE target, there's no way
to specify headers, and they won't show up in IDEs. There's workarounds
with a custom target, but that's not really great for example to set
dependencies, populate the include directory of the library or to INSTALL
the target.

Brad King said in this (http://www.cmake.org/Bug/view.php?id=15234) ticket
that posting here would be appropriate. I've posted a couple of links to
Stackoverflow in the ticket as well - other people have this issue as well
and the only way out of it is some hacks, neither of which are satisfactory.

I'm sorry if this has been been discussed before, I just joined the list,
and I couldn't find any means to search through the mailing list archives
(except through Google, whose results didn't indicate it had been discussed
before). But even if it has been discussed before, I think it's appropriate
to raise this issue again - hopefully, a fix should not be that hard.

Thanks,

Patrik
Tim Blechmann
2015-07-13 09:38:06 UTC
Permalink
Post by Patrik Huber
I've recently switched from CMake 2.8.12 to 3.2, full of enthusiasm that
header-only libraries would now be better supported through the
INTERFACE option/target. However, the support for header-only libraries
isn't really great, in particular if you're using an IDE like Visual Studio.
The main problem is that if you define an INTERFACE target, there's no
way to specify headers, and they won't show up in IDEs. There's
workarounds with a custom target, but that's not really great for
example to set dependencies, populate the include directory of the
library or to INSTALL the target.
i'm sometimes using the workaround of generating a dummy cpp file and
use a static library instead of an interface library ... ugly hack, but
allows to populate msvc/xcode/qtcreator projects ...
--
Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake-developers
蓝星灿
2015-07-13 17:19:33 UTC
Permalink
Yeah, we all do such ugly hack for header project.

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.

Yes, I’m badly want this feature, because AFAK there doesn’t exist an open
source, cross-platform build system yet to support header-only target in a
graceful way. CMake already did many things right as a C/C++ build system,
compared to many other ones, I hope Make could be the first one fulfill this.
在 2015年7月13日,下午5:38,Tim Blechmann <tim at klingt.org> 写道:
Post by Patrik Huber
I've recently switched from CMake 2.8.12 to 3.2, full of enthusiasm that
header-only libraries would now be better supported through the
INTERFACE option/target. However, the support for header-only libraries
isn't really great, in particular if you're using an IDE like Visual Studio.
The main problem is that if you define an INTERFACE target, there's no
way to specify headers, and they won't show up in IDEs. There's
workarounds with a custom target, but that's not really great for
example to set dependencies, populate the include directory of the
library or to INSTALL the target.
i'm sometimes using the workaround of generating a dummy cpp file and
use a static library instead of an interface library ... ugly hack, but
allows to populate msvc/xcode/qtcreator projects ...
--
Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/m
Brad King
2015-07-15 13:10:39 UTC
Permalink
Post by 蓝星灿
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
--
Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake-developer
Continue reading on narkive:
Loading...