在 linux 内核中添加和编译新代码

Adding and compiling new code in the linux kernel

这听起来像是一个非常菜鸟的问题。 我正在尝试在 linux 内核中实现基于 UDP 的协议。我正在关注 UDPLite 协议实现作为参考。

第 1 步

我在net/ipv4/
中创建了一个new_protocol.c 这个文件有一个函数

void _init protocol_init(void){*Code here*}

我也用过

#include "udp_impl.h"

在这个文件中,因为我使用了一些来自 UDP 协议的函数

第 2 步

我修改了文件 net/ipv4/udp_impl.h 以包含 net/new_protocol.h

第 3 步

我创建了文件 include/net/new_protocol.h,我在其中定义了函数

void protocol_init(void);

第 4 步

最后,我调用了net/ipv4/af_inet.c中的函数。此外,我在此文件中为 net/new_protocol.h

提供了包含语句

现在,当我尝试构建内核时,出现错误提示

undefined reference to `protocol_init()'

我在这里错过了什么?我包含头文件的方式不正确吗?我是否需要在 makefile 中包含一些信息才能获取新的 net/ipv4/protocol.c?

Do I need to include some info in the makefile to pick up the new net/ipv4/protocol.c?

是的,你需要。内核构建系统不会自动检测源文件,所有这些都应该在适当的 Makefile 中明确列出。在你的情况下你需要修改 net/ipv4/Makefile.

用于内核构建的 Makefile 在文件 Documentation/kbuild/makefiles.txt.

中进行了描述

我只需要在 net/ipv4/

的 makefile 中添加 protocol.o