The title say it all : I want to setup headers-only C++ (or C) library projects, but can't find a clean way.
After some searches I've found that you can't setup a normal library using add_library
to do this because it requires a compilable source file.
A way to do this would be to use add_custom_target
instead, this way :
# Get all headers (using search instead of explicit filenames for the example)
file( GLOB_RECURSE XSD_HEADERS
*.hxx
)
add_custom_target( libsxsd SOURCES ${XSD_HEADERS} )
But that don't seem to work completely here as I can't see the sources in the project generated in VS2010. I don't know if it's a bug or if I'm doing it wrong or if there is a prefered way to do this, so if you have a simple solution, please be my guest.
I think what you are looking for is just adding an include directory using the "include_directories" command for cmake.
When doing this, if it is a third party tool that you don't have control over, I would also add the "SYSTEM" flag.
So you command would look like something like this:
You can do this using the recent Interface Library feature:
This creates a library target without any source files, and adds the include directories to the INTERFACE_INCLUDE_DIRECTORIES property of the target. This means that any target that links to this library will get these directories as include paths (
-I
) when built.For instance, to use the library with an executable target, just do:
Update: CMake will soon include a library target called INTERFACE that is ideal for header-only projects. This feature is currently in the master branch. Reference.
Using the command
add_custom_target
as you propose works for me (VS2010). The files are neatly listed within my project but it has the drawback that you can't define any "Additional Include Directories" with a custom target. Instead, I now use the following:This sets up your header-only project as a dummy archive target. Don't worry, no actual binaries will be generated if you should try and build it (at least not in VS2010 and Xcode 4). The command
set_target_properties
is there because CMake will otherwise complain that it cannot infer the target language from .hpp files only.