bombela / backward-cpp

A beautiful stack trace pretty printer for C++
MIT License
3.68k stars 467 forks source link

CMake: prevent leaking of CXX_STANDARD and allow linking as regular library from add_subdirectory #241

Open cschreib opened 2 years ago

cschreib commented 2 years ago

Currently, the CMake script sets the CMAKE_CXX_STANDARD variable to 11. This leaks into parent projects when adding backward-cpp with add_subdirectory(). I have therefore modified the CMake script to explicitly set this property on each target of backward (I am not sure what is the difference between backward-object and backward, so I did it for both).

Furthermore, the documentation recommends using the add_backward() macro to use backward when adding it as a sub-directory. As far as I know, CMake best practices are that you should be able to link to a library in a unique and consistent way, regardless of where it came from (imported, or added as a sub-directory). I have therefore made modifications to the backward and backward-object targets to include the necessary library dependencies, and defined the alias target Backward::Backward to allow linking as with the find_package() way, without having to add backward.cpp to the current target:

target_link_libraries(my_app PRIVATE Backward::Backward)

This PR also updates the README accordingly.

lahwaacz commented 11 months ago

Setting CMAKE_CXX_STANDARD in a subdirectory does not "leak" the value to a parent directory, see the explanation on variable scopes in https://cmake.org/cmake/help/latest/command/set.html

As for the add_backward() macro, see my recent CMake modernization PR: #310