I have a cmake project having the structure like what is in the above image. When I run cmake, I got an error suggesting the same target already exits. My questions are how do I reserve both versions of spdlog in the project? And if reserving both versions of spdlog is not a good idea, then, how do I ensure only one of the spdlog is used?
It’s definitely not a good idea, because of the possible symbol clashes, and ODR violations.
CMake already prevents you from using both, which is good.
I always try to first check if a dependency is already provided by checking if the targets already exist + some sanity checks for the acceptable version range. If it’s there, I use it, if not, fetchcontent
Thanks for your reply. I encountered this problem and was thinking that reserving both versions of the library was reasonable(after all there are situations when old libraries are with compatibility problems and refactory is not an option), but as you mentioned, it is not a good idea and it seems like fetchcontent
can be used in that case. The solutions I found on CMP0002 error only use if(NOT TARGET)
to prevent the same target built twice, but none mentioned fetchcontent
(including GPT).