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

modern C++ library should use modern CMake #131

Open
Xeverous opened this issue Feb 26, 2021 · 0 comments
Open

modern C++ library should use modern CMake #131

Xeverous opened this issue Feb 26, 2021 · 0 comments

Comments

@Xeverous
Copy link

If you aren't aware, a bit later than modern C++ happened, modern CMake happened. Just like C++ it also kept backwards compatibilty but there are much better ways to do things now. Specifically:

libSDL2pp/CMakeLists.txt

Lines 203 to 209 in a02d5a8

IF(SDL2PP_STATIC)
ADD_LIBRARY(SDL2pp STATIC ${LIBRARY_SOURCES} ${LIBRARY_HEADERS})
ELSE(SDL2PP_STATIC)
ADD_LIBRARY(SDL2pp SHARED ${LIBRARY_SOURCES} ${LIBRARY_HEADERS})
TARGET_LINK_LIBRARIES(SDL2pp ${SDL2_ALL_LIBRARIES})
SET_TARGET_PROPERTIES(SDL2pp PROPERTIES VERSION 8.3.0 SOVERSION 8)
ENDIF(SDL2PP_STATIC)

This code currently uses old directory-based flags, which do not work well in modern CMake. As of now, any library should be used like this:

target_link_libraries(my_project PUBLIC SDL2pp)

...and nothing more. In modern CMake this single command (with new keywords: PRIVATE (build requirement) /INTERFACE (usage requirement) / PUBLIC (both)) will forward all transitive requirements of the dependency: include paths, compiler options, linker flags and so on. The requirement for this is that the target is also defined this way:

add_library(SDL2pp ...)
target_sources(SDL2pp PRIVATE ...) # sources
target_sources(SDL2pp PUBLIC ...) # headers
target_include_directories(SDL2pp PUBLIC ...)
target_compile_options(SDL2pp PRIVATE ...)
target_*(SDL2pp PUBLIC/PRIVATE ...) # and so on...

In other words, modern CMake projects do not want variables - they want targets they can link to. This is especially true for libraries that may want to use subrepository approach, which can not work in the old system.

I could make a PR for this but note: this will raise minimum required CMake version.

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

No branches or pull requests

2 participants