Best practice to configure libraries

I have a project that includes a library which I have written which can be reused in multiple projects. This library uses header files to configure it’s self depending on what the application requires. I am unsure what the best practice for this is - I want the library to be portable and to tell future users how to use it (even with minimum CMake experience)

As is the Library is added to the project as a git submodule and it’s is added via add_subdirectory in the main project. My current thinking is to have templates of the the config headers contained in the library which will be copied to a config folder in the main project (so the user can configure it and the changes will be saved in the main projects git). The submodule library’s CMakeLists.txt will then add these files to a interface library (as it’s headers only) and link this privately to the submodule library.

Are there any better ways to do this?
Thanks,
Larry

I would not use git submodule!

Try to use FetchContents or CPM.cmake.

An example for an header only lib may found here: GitHub - ClausKlein/Observe: 📣 Hey listen! A simple general-purpose event-listener system for C++17.

Hi Clause,
Thanks for your reply.

What is your reason for preferring these over submodules? Is it just so there is no dependency on git/submodules? The advantage I see with submodules is that the libraries can be developed from within a consuming project then the changes can be easily pushed.

what-are-the-advantages-and-disadvantages-for-the-use-of-git-submodules

There are 2 main reason to do it like my example:

The first one:

  • The project is desined to be used as subproject and as standalone project.
  • And the standalone library may be installed and imported with find_package().
  • Too, it is guaranteed, that the exported package in usable without errors, see test/CMakeLists.txt.

The second one:

  • With CPM.cmake, you may use a central cache for the cloned external projects.
  • Wo it is very fast, if found in cpm_source_cache.
  • And you have the option to use local installed packages if found (CPM_USE_LOCAL_PACKAGES).

see CPM.cmake options

1 Like