是否有宏定义来检查Linux内核版本? [英] Is there a macro definition to check the Linux kernel version?
问题描述
我想知道是否有一个gcc宏会告诉我Linux内核版本,所以我可以适当地设置变量类型。如果没有,我将如何去定义我自己的这个宏?
linux / version.h 文件有一个名为 KERNEL_VERSION
的宏,它可以让你根据当前的linux头文件版本( LINUX_VERSION_CODE
)已安装。例如,要检查当前的Linux头文件是否适用于内核 v2.6.16 或更早的版本: #包括< linux / version.h>
#if LINUX_VERSION_CODE< = KERNEL_VERSION(2,6,16)
...
#else
...
#endif
在运行时获取版本信息的更好方法是使用 utsname
函数在 include / linux / utsname.h 。
$ b
char * my_kernel_version = utsname() - > release;
这实质上就是 / proc / version
获取当前的内核版本。
另见
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?
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
这篇关于是否有宏定义来检查Linux内核版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!