> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man9/declare_module.9.md).

# DECLARE\_MODULE.9

`DECLARE_MODULE` — 内核模块声明宏

## 名称

`DECLARE_MODULE`

## 概要

```c
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/module.h>
```

```c
DECLARE_MODULE(name, moduledata_t data, sub, order);
DECLARE_MODULE_TIED(name, moduledata_t data, sub, order);
```

## 描述

`DECLARE_MODULE` 宏声明一个通用内核模块。它用于使用 `SYSINIT` 宏向系统注册模块。`DECLARE_MODULE` 通常在其他宏中使用，例如 [DRIVER\_MODULE(9)](/man/man9/driver_module.9.md)、[DEV\_MODULE(9)](/man/man9/dev_module.9.md) 和 [SYSCALL\_MODULE(9)](/man/man9/syscall_module.9.md)。当然，它也可以直接调用，例如用于实现动态 sysctl。

使用 `DECLARE_MODULE_TIED` 声明的模块只有在运行中的内核版本（由 `__FreeBSD_version` 指定）与构建时的内核版本完全相同时才会加载。此声明应用于依赖于稳定内核 KBI 之外接口的模块（例如依赖内部内核结构的 ABI 模拟器或 hypervisor）。当与内核一起编译时，`DECLARE_MODULE` 的行为类似于 `DECLARE_MODULE_TIED`。这允许锁和其他同步原语被安全地内联。

参数如下：

`#include <sys/kernel.h>`

**`name`** 模块名，将在 `SYSINIT` 调用中用于标识模块。

**`data`** `moduledata_t` 结构，包含两个主要项：模块的正式名称（将在 `module_t` 结构中使用）和指向类型为 `modeventhand_t` 的事件处理函数的指针。

**`sub`** 传递给 `SYSINIT` 宏的参数。有效值包含在 `sysinit_sub_id` 枚举中（参见并指定系统启动接口的类型。例如，[DRIVER\_MODULE(9)](/man/man9/driver_module.9.md) 宏在此处使用 `SI_SUB_DRIVERS` 值，因为这些模块包含设备驱动程序。对于在运行时加载的内核模块，通常使用 `SI_SUB_EXEC` 值。

**`order`** `SYSINIT` 的参数。它表示 KLD 在子系统内初始化的顺序。有效值定义在 `sysinit_elem_order` 枚举中（**sys/kernel.h**）。

## 参见

[DEV\_MODULE(9)](/man/man9/dev_module.9.md), [DRIVER\_MODULE(9)](/man/man9/driver_module.9.md), [module(9)](/man/man9/module.9.md), [SYSCALL\_MODULE(9)](/man/man9/syscall_module.9.md)

**/usr/include/sys/kernel.h**, **/usr/share/examples/kld**

## 作者

本手册页由 Alexander Langer <alex@FreeBSD.org> 编写，灵感来自 Andrew Reiter <arr@watson.org> 的 KLD Facility Programming Tutorial。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man9/declare_module.9.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
