In the Kbuild tree, when we are writing a simple hello.ko program then why do we need to use -C /lib/module/ in the build rule. Why this is required? Can it be build solely? What is the purpose of it?
-
1You're better off asking on a sister site like Superuser – Kye Nov 06 '13 at 06:10
-
[kbuild documentation](https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/kbuild), explicitly provided by answers below. Especially, [modules.txt](https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/kbuild/modules.txt), also found in your Linux kernel source directory. – artless noise Nov 07 '13 at 15:43
-
This question appears to be off-topic because it should be on unix.stachexchange.com – Martin Tournoij Mar 01 '14 at 22:28
2 Answers
while building kernel modules why do we need /lib/modules?
Its not a compulsory to give above option i.e /lib/modules
The main intention is to get configured source-code directory
.
You can set to directly configured source-code or u can provide above i.e /lib/modules/ which having softlink for built source-code.
KDIR,
you can either set full kernel source directory (configured and compiled) or just kernel headers directory (minimum needed). Two solutions
1)Full kernel sources
2)Only kernel headers (linux-headers-* packages in
Debian/Ubuntu distributions)
where in both case The sources or headers must be configured
.Many macros or functions depend on the configuration
-C
option calls the kernel Makefile
, passing the module
directory in the M variable ,the kernel Makefile knows how to compile a module
.
for e.g if you configure your kernel for Arm architecture or machine
then configured kernel Makefile
will tell how to compile and for which arhitecture your modules should be built.
To be compiled, a kernel module needs access to the kernel
headers, containing the defnitions of functions, types and
constants
.
Can it be build solely?
No you cant build solely since you module should should know for which kernel you want to build it and which configuration it needs to be compiled so that while inserting your module
All respective symbols and configuration should be matched. All this can be done through toplevel Makefile of configured kernel
.

- 3,781
- 2
- 20
- 31
To Build a module usually following command you write in the make file
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
In the above -C is used to change the directory before reading the makefile. So when make executes it go to the directory /lib/modules/$(shell uname -r)/build.
In Linux usually build is a soft link to the kernel source or source need to build a module.
From that source, make command read the kernel make file and build your module.

- 94
- 1
- 4
-
So you are saying that it is the kernel makefile that actually is called to build it. Thus, we using /lib/modules/
/build. Am I right? If it is true, why does it call kernel build tree Makefile? – dexterous Nov 06 '13 at 08:53 -
When you provide the -C option it calls the kernel Makefile better see the man page of the utility make for more clarification. – Mohit Arya Nov 06 '13 at 09:28
-
Shreyas: read this link: linux.about.com/library/cmd/blcmdl1_make.htm – Gyan Gupta Nov 07 '13 at 03:03