Home | Projects | Notes > Linux Device Drivers > Linux Kernel Module Build - In-Tree Module

Linux Kernel Module Build - In-Tree Module

 

Building an In-tree Module

 

Steps to Add In-Tree Module to Kernel Menu Configuration

  1. Create a directory in linux_bbb_4.14/drivers/char/my_c_dev/

  2. Copy main.c

  3. Create Kconfig file and add the following entries:

    CUSTOM_HELLOWORLD is an identifier by which the kernel identifies your custom module.

    User custom module can be selected or unselected. If selected, it can be either static module or dynamic module.

    Each kernel module has 3 states; y, m, n. You can specify it by using the tristate and default keyword.

    • n - Unselected by default

    • y - Select by default

  4. Add the local Kconfig entry to upper-level Kconfig:

    Go one level up (i.e., linux_bbb_4.14/drivers/char/), open Kconfig and add the following line at the end:

  5. Create a local Makefile in linux_bbb_4.14/drivers/char/my_c_dev/.

  6. Add obj-<config_item> += <module>.o to the local Makefile

    config_item - The custom module identifier (e.g., CUSTOM_HELLOWORLD)

    Since the state of this module will be selected via menu, we cannot specify it at the time of writing the Makefile.

    $(CONFIG_CUSTOM_HELLOWORLD) will be replaced by the selected state.

  7. Add the local level Makefile to higher level Makefile:

    Go one level up (i.e., linux_bbb_4.14/drivers/char/), open the Makefile and add the following line at the end:

    This is how you direct the higher-level Makefile to run another Makefile.

    -y since you want my_c_dev directory always selected.

  8. Run the Kernel Configuration:

    Select M for helloworld module support and exit saving your new configuration!

     

    kconfig-custom-module-1

    kconfig-custom-module-2

    kconfig-custom-module-3

     

  9. Open the updated linux_bbb_4.14/.config file and search for CONFIG_CUSTOM_HELLOWORLD.

    If it is there, it means that your custom module is now part of the kernel source tree.

     

    dot-config

     

  10. Build the kernel modules!

    In the Linux kernel source directory linux_bbb_4.14/ run:

    You'll be able to see main.ko generated.

  11. Check the module info and see if the built module is marked as "intree":

    Go to linux_bbb_4.14/drivers/char/my_c_dev and run:

    Then, you'll see the intree: field is marked Y.

[!] Reference: https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt