Linux-headers是用於編譯和安裝內核模塊的頭文件集。 它包含在Linux內核中,並分為多個子包,以便根據需要安裝僅適用的頭文件。
一、什麼是Linux-headers
作為內核開發者或驅動程序開發者,必須了解Linux系統中的頭文件。 由於Linux內核源代碼非常龐大,因此僅提供內核源代碼並不足以編寫內核模塊或設備驅動程序。內核源代碼中的許多函數,變數和結構需要與其他文件進行交互,其中一些位於內核的其他部分中,而其他則是來自其他軟體包,例如標準C庫。
因此,Linux內核源代碼中的頭文件是編寫內核模塊的重要一環。Linux-headers提供了一個集合,其中包括內核開發和驅動程序開發中的所有必需頭文件。
二、Linux-headers的分析
在Linux-headers包中,有許多不同的頭文件,每個頭文件都有不同的功能。接下來,我們將一一介紹。
1. asm-header
asm-header需要被包含,以便在開發內核或內核模塊時使用指令集相關的C編譯器內建函數,該文件包含了大量必需的支持體系結構的函數。
/*
* asm-generic/atomic.h
*
* Atomic operations that C can't guarantee us. Useful for
* resource counting etc..
*/
#define ATOMIC_INIT(i) { (i) }
typedef struct {
int counter;
} atomic_t;
#define atomic_read(v) ((v)->counter)
#define atomic_set(v,i) (((v)->counter) = (i))
2. linux-header
linux-header是標準的Linux內核頭文件補丁,提供了許多必要的結構和函數,包括內核參數、標準IO頭文件和內存分配函數等。
/*
* linux-kernel.h
*
* This file contains most of the kernel definitions. It includes some
* headers to define things which are needed widely across the kernel.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
3. kernel-header
kernel-header提供了大量的Linux內核函數,許多內存管理和分配函數都在這個頭文件中定義。
/*
* kernel.h
*
* General kernel definitions and macros.
*
* Developed by Linus Torvalds
* More information in the accompanying file COPYING.
*/
#ifndef _LINUX_KERNEL_H
#define _LINUX_KERNEL_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#endif
三、為什麼要使用Linux-headers
要為Linux系統編寫內核模塊或設備驅動程序,必須使用Linux-headers。 如果您未安裝Linux-headers,則無法編譯內核模塊或驅動程序,因為缺乏系統定義的所有必需頭文件。然而,在使用Linux-headers時,您可以避免許多已知的問題,例如:未能正確更新您的內核和頭文件。
另外,應該始終使用匹配內核版本的Linux-headers。 當Linux內核更新時,新的內核很可能需要新的頭文件。用決不兼容的版本安裝頭文件會在您試圖編譯內核模塊時引發各種編譯問題。
四、如何安裝Linux-headers
在大多數Linux發行版中,安裝方式如下:
$ sudo apt-get install linux-headers-$(uname -r)
在CentOS中,安裝方式如下:
$ sudo yum install kernel-devel
在其他發行版中,安裝方式類似,請參考官方文檔。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/248373.html