Skip to content

Defining BUILD_SHARED_LIBS option may break superproject build when using add_subdirectory #2263

@vadz

Description

@vadz

I was surprised to discover that adding add_subdirectory(cpp-httplib) to our project CMakeLists.txt has broken the build because some libraries that were supposed to be built as shared have suddenly started being built as static. The reason for this is that this library defines BUILD_SHARED_LIBS as OFF and, as CMake documentation says:

Note that if bringing external dependencies directly into the build, such as with FetchContent or a direct call to add_subdirectory(), and one of those dependencies has such a call to option(BUILD_SHARED_LIBS ...), the top level project must also call option(BUILD_SHARED_LIBS ...) before bringing in its dependencies.

and our project doesn't define this option. We probably could define it, but I think it would be better if cpp-httplib didn't define it instead, especially when HTTPLIB_COMPILE is OFF, i.e. when it's not even used by this library itself. I believe the best approach would be to use this option as default value for some new HTTPLIB_SHARED option and only use this option to decide which kind of library to build. This would have the following desirable consequences:

  1. BUILD_SHARED_LIBS will still be respected if it's defined (by the superproject or directly on the command line when configuring).
  2. It will become possible to specify whether static or shared cpp-httplib library should be built by setting HTTPLIB_SHARED without affecting anything else.
  3. Adding this project to the existing build won't break anything else any more.

Please let me know if you'd like me to submit a PR implementing this proposal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions