How to detect Linux distro type?

My CMake project targets Ubuntu and Centos distros and I need to link to separate libraries for each. How can I detect the distro type and conditionally call target_link_libraries accordingly?

There are some suggestions here: https://stackoverflow.com/questions/26919334/detect-underlying-platform-flavour-in-cmake but the discussion is quite old.

I guess I could define a variable for it that must be set in the CMake invocation but that is not very elegant.

You can extract information from /etc/os-release pretty easily for recent distro builds at least.

Out of curiosity, what different libraries do you need to link to based on the distro?

Thanks for your reply. The library is SystemC, which we build from source. TBH I don’t know whether or not a library built on Ubuntu will work on Centos or vice versa.

It…can. Depends on what features of the standard library is in use, but in general builds on “older” distros should work reasonably well on “newer” distros if everything above the standard library and “core” things like X are bundled. Determining what is “core” and not is kind of a fine art though.

Thanks. I’ll have another look at it.