c++linux

Macro to determine linux distribution


I'd like to add a Macro based on the linux distribution, for example, Ubuntu 15 vs Ubuntu 14LTS. So within an ifdef for __linux I would perform the check:

#ifdef __linux
    // find version
#endif

Is there a smart way to do this? Thanks for any ideas


Solution

  • I'd just write something external to C++ to define the macros for you. I don't see anything in Boost.Predef that gives you anything more than just linux. Something like:

    linux_defines() {
        if -f /etc/redhat-release; then
            awk '{
                printf("-D__rhel_ver__=%d\n",$7 * 100)
            }' /etc/redhat-release
        elif -f /etc/lsb-release; then
            awk -F= '$1 == "DISTRIB_RELEASE"{
                printf("-D__ubuntu_ver__=%d\n",$2 * 100)
            }' /etc/lsb-release
        elif ...
             ...
        fi
    }
    

    And stick a call to that in your makefile to extend your CPPFLAGS (or CXXFLAGS or... )