Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to get the linux kernel version in cpp program

how to get the linux kernel version in cpp program

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++linuxhelptutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scamguru
    wrote on last edited by
    #1

    I have a problem like i need to include a different header files depending on the linux kernel in a .cpp file. I have to check the linux kernel version and include the headerfile depending on that kernel. How can I do that? I have ARCH variable in Makefile, can i make use of that? :omg:

    P 1 Reply Last reply
    0
    • S scamguru

      I have a problem like i need to include a different header files depending on the linux kernel in a .cpp file. I have to check the linux kernel version and include the headerfile depending on that kernel. How can I do that? I have ARCH variable in Makefile, can i make use of that? :omg:

      P Offline
      P Offline
      ParagPatel
      wrote on last edited by
      #2

      Best way it to first include keranal version.h file in your project (ex:"/usr/include/linux/version.h"), and then put below checks in your code.

      #if RHEL_MAJOR == 5

      #include "xyx_5.h"

      #else

      #include "xyx.h"

      #endif

      Parag Patel Sr. Software Eng, Varaha Systems

      S 1 Reply Last reply
      0
      • P ParagPatel

        Best way it to first include keranal version.h file in your project (ex:"/usr/include/linux/version.h"), and then put below checks in your code.

        #if RHEL_MAJOR == 5

        #include "xyx_5.h"

        #else

        #include "xyx.h"

        #endif

        Parag Patel Sr. Software Eng, Varaha Systems

        S Offline
        S Offline
        scamguru
        wrote on last edited by
        #3

        lOOKS LIKE this doesn't work for me. I saw the version.h file, it has got 3 lines as below. #define UTS_RELEASE "2.4.20" #define LINUX_VERSION_CODE 132116 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) And I want to differentiate kernels for 2.4 and 2.6. Is the above code still works with it.I've placed my code as below, but it seems not working. #include "/usr/include/linux/version.h" #if RHEL_MAJOR=2.4.20 #include "../../build/i686-Linux-2.4.32/ReleaseVersion.hh" #else #include "../../build/i686-Linux-2.6.21/ReleaseVersion.hh" #endif What went wrong in my case. Thanks in advance :wtf:

        P 1 Reply Last reply
        0
        • S scamguru

          lOOKS LIKE this doesn't work for me. I saw the version.h file, it has got 3 lines as below. #define UTS_RELEASE "2.4.20" #define LINUX_VERSION_CODE 132116 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) And I want to differentiate kernels for 2.4 and 2.6. Is the above code still works with it.I've placed my code as below, but it seems not working. #include "/usr/include/linux/version.h" #if RHEL_MAJOR=2.4.20 #include "../../build/i686-Linux-2.4.32/ReleaseVersion.hh" #else #include "../../build/i686-Linux-2.6.21/ReleaseVersion.hh" #endif What went wrong in my case. Thanks in advance :wtf:

          P Offline
          P Offline
          ParagPatel
          wrote on last edited by
          #4

          Try below code,

          #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,0)
          //include for version 2.4 or lower
          #else
          #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
          //include for version 2.6 or higer
          #endif // 2.4
          #endif //2.6

          Parag Patel Sr. Software Eng, Varaha Systems

          S 1 Reply Last reply
          0
          • P ParagPatel

            Try below code,

            #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,0)
            //include for version 2.4 or lower
            #else
            #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
            //include for version 2.6 or higer
            #endif // 2.4
            #endif //2.6

            Parag Patel Sr. Software Eng, Varaha Systems

            S Offline
            S Offline
            scamguru
            wrote on last edited by
            #5

            Thanks a lot man. It's working.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups