while building kernel modules why do we need /lib/

2019-06-11 08:15发布

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?

2条回答
ら.Afraid
2楼-- · 2019-06-11 08:40

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楼-- · 2019-06-11 08:59

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.

查看更多
登录 后发表回答