Welcome to the Linux Foundation Forum!

What is the difference between compiling at the directory level and compiling a module?

I am currently in Lesson 10: Kernel and Driver Building, Loading and Dependencies and I tried changing the option CONFIG_USB driver as module (the module name is usbcore) and then changing drivers/usb/core/usb.c by adding pr_info(...) in usb_init().

After that, I tried compiling at the directory level with command:

make drivers/usb/core/

and do make modules_install, rmmod usbcore (assumming usbcore already loaded previously), modprobe usbcore, but the message is not in dmesg.

And then, I tried compiling the module with command:

make M=drivers/usb/core

and do make modules_install, rmmod usbcore (assumming usbcore already loaded previously), modprobe usbcore, and the message appear in dmesg.

So I am curious, what is the difference between compiling at the directory level and compiling a module, even though we use the same path?

Another information is that I am using virtual machine to try things out, in case there's some difference whether using virtual machine or actual machine.

Thank you.

Best Answer

  • bruhtus
    bruhtus Posts: 2
    edited June 9 Answer ✓

    From this documentation:
    https://www.kernel.org/doc/html/latest/kbuild/modules.html

    It looks like if we want to install the module using kbuild (the build system used by linux kernel), we need to use M=<dir> option in the make command. So if we just use make <dir> and not make M=<dir>, the kbuild will only build the object file (.o file) and not the module file (.ko file).

    The module file (.ko file) is a loadable kernel module that can be dynamically loaded and unloaded to a running kernel without restarting the computer. The object file (.o file) serve as the building blocks for creating the module file[1].

    [1] https://stackoverflow.com/a/76345074

Categories

Upcoming Training