Kernel: how to add a new source file for building a kernel?

For an academic project, I want to add the source file (myfile.c) to the kernel/ directory, the same directory as exit.c and fork.c The build system does not appear automatically to get a new file, since I click on the "undefined reference" function links defined in myfile.c . How can I include this file?

+4
source share
2 answers

You need to add the appropriate object file to kernel/Makefile . If you have a configuration variable for your code, you should use:

 obj-$(CONFIG_ZERO_STIMULUS_FEATURE) += zerostimulus.o 

If you create code without a configuration variable, you simply add it to the obj-y variable:

 obj-y += zerostimulus.o 

The configuration variable expands to y , m or n , depending on whether the function is built-in, whether it is built as a module, or turned off. Then the variables obj-y , obj-m .

+5
source

It is right!

Just an addendum: before creating the kernel, you know that when you run " make menuconfig " you can set which functions will be built into the kernel image ( y ), which will be included as a loadable module ( m ) and which will not be included ( n ).

if you want to install it for your new function, you can edit the Kconfig file, which you will find in the same folder of the new file. In your case, " linux-xyz/kernel/Kconfig "

This is an example:

 config ZERO_STIMULUS_FEATURE tristate "My new feature" default m ---help--- This is my brand new feature Here a multi-line description 

Typically, the error is setting " config CONFIG_ZERO_STIMULUS_FEATURE " instead of " config ZERO_STIMULUS_FEATURE ": do not add the prefix " CONFIG_ " here

If your function does not load as a module, you will need to use the <<26> keyword instead of " tristate ". Take a look at the other Kconfig files and you will also see how easy it is to install dependencies.

Regards, / Angel

+2
source

Source: https://habr.com/ru/post/1386897/


All Articles