I am developing my own header-only library that I want to use as a framework in other software. I am using CMake
for setting up demo targets, tests, and so on.
However, I am unsure how to deal with dependencies of my library. Currently, I am using the following approach:
FIND_PACKAGE( foo )
I have a configuration config/foo.hh.in
file for foo
that checks whether the package exists:
#ifndef FOO_HH__
#define FOO_HH__
#cmakedefine HAVE_FOO
#endif
In the main CMakeLists.txt
, I do:
CONFIGURE_FILE( ${CMAKE_SOURCE_DIR}/include/config/foo.hh.in ${CMAKE_SOURCE_DIR}/include/config/foo.hh )
In the code, I can thus do:
#include "config/foo.hh"
#ifdef HAVE_FOO
// Code that is specific to having the `foo` library available
#endif
The problem with this approach is that it only works once, namely when the library is installed for the first time and all the dependencies are being checked. Since I am not shipping the optional dependencies with my library, I am wondering whether this is a good approach. What if the user decides to install an optional dependency after installing my library? In this case, I will not be able to detect the presence of the library.
How do you usually solve this issue? What am I doing wrong with this approach?