无论我在哪里搜索,我都可以找到创建Linux内核模块的答案。示例
/*
* hello−1.c − The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}这里有init_module和cleanup_module函数,我理解它们包含初始化和清理内核时要执行的内容。通过在makefile中添加obj-m += hello-1.c来实现。
但我不想这样。我想添加一个内置程序,而不是驱动程序,基本上是一个服务,以方便云端上传一些来自内核级别的数据。在编译内核时,我不想要程序的模块选项。
我理解只是程序,我应该使用obj-y而不是obj-m。但是没有编写这种程序的手册。为什么?我是不是遗漏了什么?这些程序是否也具有init_module和cleanup_module功能,即使它们不是模块?
发布于 2015-01-13 07:14:10
例如,假设您的源代码在linux内核源代码树中的driver/new下。您需要在drivers和new下修改new来静态地将模块构建到Linux内核中。
在drivers/Makefile下,将下面一行添加到末尾。
obj-y += new/在drivers/new/Makefile下,将下面一行添加到末尾。
obj-y += hello.o在构建linux内核之后。并加载以查看您的模块已经使用printk命令打印了dmesg消息。
注意:当静态地将模块构建到linux中时,请更改
int init_module(void)至
int __init init_module(void)和改变
void cleanup_module(void)至
void __exit cleanup_module(void)发布于 2015-01-13 08:44:10
请参阅:
The kbuild Makefile specifies object files for vmlinux
in the $(obj-y) lists. These lists depend on the kernel
configuration.
Kbuild compiles all the $(obj-y) files. It then calls
"$(LD) -r" to merge these files into one built-in.o file.
built-in.o is later linked into vmlinux by the parent Makefile.
The order of files in $(obj-y) is significant. Duplicates in
the lists are allowed: the first instance will be linked into
built-in.o and succeeding instances will be ignored.
Link order is significant, because certain functions
(module_init() / __initcall) will be called during boot in the
order they appear. So keep in mind that changing the link
order may e.g. change the order in which your SCSI
controllers are detected, and thus your disks are renumbered.
Example:
#drivers/isdn/i4l/Makefile
# Makefile for the kernel ISDN subsystem and device drivers.
# Each configuration option enables a list of files.
obj-$(CONFIG_ISDN_I4L) += isdn.o
obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o“
https://stackoverflow.com/questions/27916116
复制相似问题