Building a kernel module from several source files which one of them has the same name as the module

Asked
Active3 hr before
Viewed126 times

6 Answers

buildingfilesmodule
90%

As per my understanding it is not possible to have the module name and the source name to be the same. It would be better to provide module name as module.o and use the Makefile for compiling loadable kernel module as shown below,, Meta Stack Overflow ,Stack Overflow en español,Stack Overflow em Português

Makefile
src/mymodule.c
src/mymodule_func.c

#Makefile
obj - m += mymodule.o
mymodule - objs: = . / src / mymodule.o. / src / mymodule_func.o

all:
   make - C $(KERNEL_PATH) M = $(PWD) modules

clean:
   make - C $(KERNEL_PATH) M = $(PWD) clean
load more v
88%

Is it possible to build a kernel module from several source files which one of them has the same name as the module?,For example: I want to build "mymodule.ko" with the following source files: mymodule.c mymodule_func.c,What you're looking for is the Linux Kernel Module Programming Guide, specifically the section on the /proc filesystem, which has well documented examples of how to add new entries.,From the other side, paths enumerated in *-objs variable should be relative (this is requirement by Kbuild). So you need to strip prefix from these absolute paths:

This makefile doesn't work:

#Makefile
obj - m += mymodule.o
mymodule - objs: = mymodule.o mymodule_func.o
load more v
72%

When building an external module, only a subset of the "make" targets are available.,It is possible to build single files that are part of a module. This works equally well for the kernel, a module, and even for external modules.,The kbuild system will build <module_name>.o from <module_name>.c, and, after linking, will result in the kernel module <module_name>.ko. The above line can be put in either a "Kbuild" file or a "Makefile." When the module is built from multiple sources, an additional line is needed listing the files:,The command to build an external module is:

$ make -C <path_to_kernel_src> M=$PWD
load more v
65%

It is possible to build single files that are part of a module. This works equally well for the kernel, a module, and even for external modules.,The kbuild system will build <module_name>.o from <module_name>.c, and, after linking, will result in the kernel module <module_name>.ko. The above line can be put in either a “Kbuild” file or a “Makefile.” When the module is built from multiple sources, an additional line is needed listing the files:,Although there is no distinction between the ordinary source files and the binary file, kbuild will pick up different rules when creating the object file for the module.,Then to install the module(s) just built, add the target “modules_install” to the command:

$ make -C <path_to_kernel_src> M=$PWD
load more v
75%

An object file from the kernel source directory that describes the environment a module was built for.,We are getting closer to looking at some actual module code. But first, we need to look at some other things that need to appear in your module source files. The kernel is a unique environment, and it imposes its own requirements on code that would interface with it.,Every nontrivial module also requires a cleanup function, which unregisters interfaces and returns all resources to the system before the module is removed. This function is defined as:,A makefile symbol used by the kernel build system to determine which modules should be built in the current directory.

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
    printk(KERN_ALERT "Hello, world\n");
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);
% make
make[1]: Entering directory `/usr/src/linux-2.6.10'
  CC [M]  /home/ldd3/src/misc-modules/hello.o
  Building modules, stage 2.
  MODPOST
  CC      /home/ldd3/src/misc-modules/hello.mod.o
  LD [M]  /home/ldd3/src/misc-modules/hello.ko
make[1]: Leaving directory ` / usr / src / linux - 2.6 .10 ' %
   su
root # insmod. / hello.ko
Hello, world
root # rmmod hello
Goodbye cruel world
root #
load more v
40%

In this case you have to specify the relative paths of the source files in the Makefile like this:,If you have a single file in your module, say samplefile.c, then the Makefile should look like,In the above example, you had to specify the source file name in “obj-m += samplefile.o”. As a result the generated kernel module name, samplefile.ko, was also same as the source file name. But you might want different name of you kernel module from your source file. To do that you have to modify your Makefile like this.,After comilation, samplefile.ko will be generated in your source directory.

To install the linux-header package on Debian or Ubuntu Linux run this command as root.

# apt - get install linux - headers - $(uname - r)
load more v

Other "building-files" queries related to "Building a kernel module from several source files which one of them has the same name as the module"