yaml-cpp
yaml-cpp copied to clipboard
CMake: handle backward compatibility
Some versions of yaml-cpp require to link cmake targets with yaml-cpp,
while more recent version require to link them with yaml-cpp::yaml-cpp.
Is there a way to handle backward compatibility in CMake so it would work in both cases ? Many Linux distros still ship with versions that require yaml-cpp.
Many thanks
Well, I haven't tried it with yaml-cpp, but I have had to deal with this problem before. Here's a snippet from that:
find_package(date CONFIG NAMES date howardhinnant-date REQUIRED)
message(STATUS "Found and using libdate.")
if(TARGET howardhinnant-date::howardhinnant-date-tz)
add_library(date::date-tz ALIAS howardhinnant-date::howardhinnant-date-tz)
endif()
I imagine you can use something similar.
Thanks :)
We ended up using a similar approach:
if (TARGET yaml-cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE yaml-cpp)
else()
target_link_libraries(${PROJECT_NAME} PRIVATE yaml-cpp::yaml-cpp)
endif()
This should probably documented somewhere.
The advantage of creating the ALIAS library is if you ever need to use it more than one place in your build system, you only have to have the conditional in one place.