是否有宏定义来检查 Linux 内核版本? [英] Is there a macro definition to check the Linux kernel version?

查看:39
本文介绍了是否有宏定义来检查 Linux 内核版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个 gcc 宏可以告诉我 Linux 内核版本,以便我可以适当地设置变量类型.如果没有,我将如何定义自己的宏来执行此操作?

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?

推荐答案

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

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

在运行时获取版本信息的更好方法是使用 include/linux/utsname.h.

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;

这基本上是 /proc/version 获取当前内核版本的方式.

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

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

这篇关于是否有宏定义来检查 Linux 内核版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆