How to Compile a Module in Linux

Many functions provided by the Linux operating system are implemented within the "kernel" -- the part of Linux that always remains in main memory. However, Linux has an extensible architecture; you can have modules that are outside the kernel and get loaded whenever they are needed. The most common purpose of a module is to provide drivers for a given device or device type. Since Linux is an open-source operating system, you can compile the appropriate source codes to build a executable module that can be loaded by the kernel.

Instructions

    • 1

      Log in to your Linux computer to bring up a text-mode console (also called a "shell"). The specific way of performing this step depends on your Linux distribution. For example, on Ubuntu Linux, press "Control-Alt-F1" at the login screen to bring up a text-mode console. Type your user name and password to log in. Ubuntu will present you with a shell ready to accept your commands.

    • 2

      Change the shell's working directory to the location of your module's source code by typing the following command into the shell:

      cd /home/lyle/devDir/myMod

      Replace "/home/lyle/devDir/myMod" by the full path to the module's source code directory. Press "Enter."

    • 3

      Launch a text editor to create a new file named "Makefile." Insert the following text into the new file:

      obj-m = myDriver.o

      KERNEL = $(shell uname -r)

      all:

      make -C /lib/modules/$(KERNEL)/build M=$(PWD) modules

      clean:

      make -C /lib/modules/$(KERNEL)/build M=$(PWD) clean

      Replace "myDriver.o" with the name of the compiled module file you are trying to build. Save the modifications to the file and exit the text editor.

    • 4

      Compile the module by typing the following command into the shell:

      make

      Press "Enter." After a delay that depends on the amount of code Ubuntu needs to compile and the speed of your computer's CPU, the shell will prompt you for the next command and the compiled module will be in file "myDriver.ko" in the current directory.

Related Searches:

References

Comments

Related Ads

Featured