catchorg / Catch2

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
https://discord.gg/4CWS9zD
Boost Software License 1.0
18.48k stars 3.02k forks source link

Catch2 Builds as Shared library when parent CMake sets `BUILD_SHARED_LIBS` #2895

Open AidanBeltonS opened 1 month ago

AidanBeltonS commented 1 month ago

Describe the bug Catch 2 builds its libraries as a shared library, when using BUILD_SHARED_LIBS=ON and pulling the repo via FetchContent.

Expected behavior It seems the v3 library is supposed to work only as a static library. Correct me if I am wrong. So regardless of the project that uses Catch2, the Catch2 library should build as a static not shared library.

If this is expected behaviour, please close this issue.

Reproduction steps

$ git clone https://github.com/AidanBeltonS/Catch2_reproducer
$ cd Catch2_reproducer
$ mkdir build
$ cd build
$ cmake -G Ninja .. -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang
$ ninja
$ find -name "*.so"
./_deps/catch2-build/src/libCatch2Main.so
./_deps/catch2-build/src/libCatch2.so
./libMain_Object.so

Additional context I have not encountered any specific problem with it being a shared library, however it seems like an error to have the parent CMake effect the Catch build in this way.

horenmar commented 3 weeks ago

Catch2 can be built as a shared lib, but I don't try to support it past using CMake-provided utility to make every symbol visible -> if that fails, the user is on their own.

As for BUILD_SHARED_LIBS, Catch2 tries to use the standard CMake configuration options. This does mean that if you include it as a subproject, it will use the same configuration, because it is global. If you need something different, you have to undo it yourself.