[CMake] Visual Studio support for Header Only Libraries

Casagrande Raffael raffael.casagrande at sam.math.ethz.ch
Tue Jun 2 09:54:56 EDT 2015


Hello,

How should I properly setup a header-only library in cmake such that:
1. The headers appear in the Visual Studio solution.
2. In an executable i can use target_link_libraries(<TARGET> <HEADER_ONLY_LIBRARY>) to specify that I want to use (the headers of) this library.

So far I have considered two options:
a) create a INTERFACE library using add_library(<HEADER_ONLY_LIBRARY> INTERFACE), this fulfills property 2. from above but not 1.
b) create a usual library and set LINKER_LANGUAGE to CXX. In this way 1. works but 2. doesn't work unfortunately because the executable will try to link to a library that does not exist.

Notes:
- A not so nice workaround is to go with b) and add an empty source file to it.
- Another option would be to not link the executable with the HEADER_ONLY_LIBRARY. However one thing that I like about target_link_libraries is that all the "usage requirements" from the HEADER_ONLY_LIBRARY (such as include directories and compile options) are applied to the executable automatic. So if I don't use target_link_libraries I would have to set these options manually (unless there is another alternative?).

I would be very happy for some suggestions :)

Raffael

P.S.: See also the (a bit outdated) discussion http://stackoverflow.com/a/11583676/2796908.http://stackoverflow.com/a/11583676/2796908.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150602/66285317/attachment.html>


More information about the CMake mailing list