是否有宏定义来检查Linux内核版本?

时间:2022-03-16 07:04:02

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 macro that does this?

我想知道是否有一个gcc宏可以告诉我Linux内核版本,这样我就可以适当地设置变量类型。如果不是,我该如何定义我自己的宏?

2 个解决方案

#1


54  

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:

linux /版本。h文件有一个名为KERNEL_VERSION的宏,它可以让你检查你想要的版本和当前的linux header版本(LINUX_VERSION_CODE)。例如,检查当前的Linux头文件是否用于内核v2.6.16或更早的版本:

#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.

在运行时获得版本信息的更好方法是使用包含/linux/utsname.h中的utsname函数。

char *my_kernel_version = utsname()->release;

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

这就是/proc/version获得当前内核verison的方式。

See also

Getting kernel version from linux kernel module at runtime

在运行时从linux内核模块获得内核版本

#2


1  

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

gcc不知道这个信息。作为一种替代方法,您可以在运行时轻松地确定许多内核信息。

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.

并让代码在运行时查看/proc/version(或您在内核运行时环境中需要的任何东西)并正确地设置kernel_version。kernel_x_foo_type等是您希望在内核版本上有条件的类型。调用代码需要查看kernel_version并访问适当的u。k_x u。k_y或u。k_z数据。

#1


54  

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:

linux /版本。h文件有一个名为KERNEL_VERSION的宏,它可以让你检查你想要的版本和当前的linux header版本(LINUX_VERSION_CODE)。例如,检查当前的Linux头文件是否用于内核v2.6.16或更早的版本:

#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.

在运行时获得版本信息的更好方法是使用包含/linux/utsname.h中的utsname函数。

char *my_kernel_version = utsname()->release;

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

这就是/proc/version获得当前内核verison的方式。

See also

Getting kernel version from linux kernel module at runtime

在运行时从linux内核模块获得内核版本

#2


1  

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

gcc不知道这个信息。作为一种替代方法,您可以在运行时轻松地确定许多内核信息。

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.

并让代码在运行时查看/proc/version(或您在内核运行时环境中需要的任何东西)并正确地设置kernel_version。kernel_x_foo_type等是您希望在内核版本上有条件的类型。调用代码需要查看kernel_version并访问适当的u。k_x u。k_y或u。k_z数据。