Is there a macro definition to check the Linux kernel version?

后端 未结 2 1754
长发绾君心
长发绾君心 2020-11-29 01:59

I\'m wondering if there is a gcc macro that will tell me the Linux kernel version so I can set variable types appropriately. If not, how would I go about defining my own mac

相关标签:
2条回答
  • 2020-11-29 02:24

    gcc won't know this information. As an alternative, you can determine a lot of kernel information at runtime easily.

    You can define your runtime type like

    struct unified_foo {
         unsigned int kernel_version;
         union {
             kernel_x_foo_type k_x;
             kernel_y_foo_type k_y;
             kernel_z_foo_type k_z;
         } u;
    };
    

    and have code at runtime look at /proc/version (or whatever you need from the kernel runtime environment) and set kernel_version approriately. The kernel_x_foo_type et al. is your type that you want to be conditional on the kernel version. The calling code needs to look at kernel_version and access the appropriate u.k_x, u.k_y, or u.k_z data.

    0 讨论(0)
  • 2020-11-29 02:46

    The linux/version.h file has a macro called KERNEL_VERSION which will let you check the version you want against the current linux headers version (LINUX_VERSION_CODE) installed. For example to check if the current Linux headers are for kernel v2.6.16 or earlier:

    #include <linux/version.h>
    
    #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,16)
    ...
    #else
    ...
    #endif
    

    A better way to get the version information at run-time is to use the utsname function in include/linux/utsname.h.

    char *my_kernel_version = utsname()->release;
    

    This is essentially how /proc/version gets the current kernel verison.

    See also

    Getting kernel version from linux kernel module at runtime

    0 讨论(0)
提交回复
热议问题