LINUX_VERSION_CODE与KERNEL_VERSION

As the Linux version is constantly updated, when the device driver is compatible with different versions of the kernel, it is necessary to know the current kernel source code version in use to call the corresponding version of the kernel API. These two macros are defined in the file

/usr/include/linux/version.h

#define LINUX_VERSION_CODE 263213
#define KERNEL_VERSION(a,b,c) (((a)<<16)+((b)<<8)+(c))

After installing linux-header, the version.h file was found in the following directory

/usr/lib/modules/4.4.45-1-MANJARO/build/include/generated/uapi/linux/version.h

The Linux system used is MANJARO, the kernel version number is 4.4.45, and the corresponding LINUX_VERRSION_CODE is 263213. The calculation method is as follows:

First convert 4.4.45 to hexadecimal to 0x04.0x04.0x2D,
and then expand the macro KERNEL_VERSION (0x04, 0x04, 0x2D) to get the hexadecimal number
04042D. Finally, convert 0x040423 to decimal to get the decimal 263213, which is LINUX_VERSION_CODE value 263213

You can use the following code to call different API functions according to the linux kernel version number:

#if LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0)
....//3.10.0 之前的API调用
#else 
....//3.10.0 版本之后的API调用
#endif 

Guess you like

Origin blog.csdn.net/sun172270102/article/details/107166784