Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kernel: how to add a new source file for kernel build?

Tags:

kernel

For an academic project, I'm looking to add a source file (myfile.c) to the kernel/ directory, the same directory as exit.c and fork.c. The build system does not appear to automatically pick up the new file, as I'm hitting "undefined reference" link errors to functions defined in myfile.c. How could I get this file incorporated?

like image 900
zer0stimulus Avatar asked Dec 18 '11 05:12

zer0stimulus


People also ask

How to build and install Linux kernel using make command?

*** Execute 'make' to start the build or try 'make help'. Now the kernel is configured and you can build and install it by command: The first make command compile the source code files. The -j option make make compile in parallel using concurrent tasks of at most number of the processors in the system so that the compilation can be much faster.

How do I make sure a Linux kernel contains the correct modules?

Also, the kernel must have been built with modules enabled. If you are using a distribution kernel, there will be a package for the kernel you are running provided by your distribution. An alternative is to use the “make” target “modules_prepare.” This will make sure the kernel contains the information required.

How to change the default configuration of the Linux kernel source code?

The Linux kernel source code comes with the default configuration. However, you can adjust it to your needs. To do so, follow the steps below: 1. Navigate to the linux-5.9.6. directory using the cd command: 2. Copy the existing configuration file using the cp command: 3. To make changes to the configuration file, run the make command:

What is a kernel makefile?

The kernel Makefiles are designed to be run with GNU Make. The Makefiles use only the documented features of GNU Make, but they do use many GNU extensions. GNU Make supports elementary list-processing functions. The kernel Makefiles use a novel style of list building and manipulation with few “if” statements.


1 Answers

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

obj-$(CONFIG_ZERO_STIMULUS_FEATURE) += zerostimulus.o

If you're building in your code without a configuration variable, then you'll just add it to the obj-y variable:

obj-y += zerostimulus.o

The configuration variable expands to y, m, or n, depending if the feature is built-in, built as a module, or turned off. Then the obj-y, obj-m, variables are built.

like image 50
sarnold Avatar answered Oct 12 '22 17:10

sarnold