Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cschreib
Copy link

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
Copy link
Contributor

lahwaacz commented Sep 2, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants