# How to detect Linux distro type?

**URL:** https://discourse.cmake.org/t/how-to-detect-linux-distro-type/2476
**Category:** Usage
**Created:** [January 5, 2021, 12:27pm UTC](https://discourse.cmake.org/t/how-to-detect-linux-distro-type/2476 "2021-01-05T12:27:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [January 5, 2021, 12:27pm UTC](https://discourse.cmake.org/t/how-to-detect-linux-distro-type/2476/1 "2021-01-05T12:27:44Z")

</div>

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](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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 5, 2021, 1:54pm UTC](https://discourse.cmake.org/t/how-to-detect-linux-distro-type/2476/2 "2021-01-05T13:54:54Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [January 5, 2021, 1:57pm UTC](https://discourse.cmake.org/t/how-to-detect-linux-distro-type/2476/3 "2021-01-05T13:57:22Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 5, 2021, 2:01pm UTC](https://discourse.cmake.org/t/how-to-detect-linux-distro-type/2476/4 "2021-01-05T14:01:50Z")

</div>

> [@DavidA](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![DavidA](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davida/32/128_2.png) [@DavidA](https://discourse.cmake.org/u/DavidA)
#### Post date: [January 5, 2021, 2:03pm UTC](https://discourse.cmake.org/t/how-to-detect-linux-distro-type/2476/5 "2021-01-05T14:03:16Z")

</div>

Thanks. I’ll have another look at it.
