Header-only library using Swift Package Manager

Consider a "library" that consists of only C++ header files: no C++ source files. I wish to create that as a "library" inside my package, so that other real libraries I am building can find these header files. Additionally, anyone who uses my package will need to pick up these headers on the search path.

I have found that if I gratuitously add a single .cpp file into the directory containing my headers files, then SPM will use the path to this directory by downstream components (i.e. adding the requisite -I flags to the compile line that lead to the header directory) but I cannot figure out what to do when I have no source files at all (i.e. without source files, the -I lines are not generated, presumably because SPM doesn't consider that I've created a library).

Is there something other than a "library" I should be making? Should I be doing this using modulemaps (and if so how)?

Given that so many C++ libraries are header-only (no source) I cannot be the first person to want this, but I've scoured the web and found nothing so far. Help?

1 Like

Just add the empty cpp file, that's what we do in Swift Numerics, for example (it's a C file, but same: https://github.com/apple/swift-numerics/blob/main/Sources/_NumericsShims/_NumericsShims.c).

I think it's worth requesting support for this (bugs.swift.org), but in the meantime, the workaround is happily trivial.

1 Like

That's not an option here. The directories are getting mapped in via a git submodule, and we can't touch the source it's coming from.

Unless there's a way to add a source file from a completely different directory, out of left field, but I can't see how to do that either.

Map the git submodule into the include directory, put an empty C file in the source directory that wraps it?

1 Like
Terms of Service

Privacy Policy

Cookie Policy