Linux Device Driver Programming Lecture 24- How to create Makefile

  • Post author:
  • Post category:Blog

 

How to create Makefile

 

 

In the previous article, we built our kernel module and tested it on the target. We used this long command(shown in Figure 1) in order to build our module.

In this guide, we will streamline the process by creating a Makefile to automate the module build. Here’s a step-by-step guide to creating the Makefile:

Building the kernel module against target
Figure 1. Building the kernel module against target

Instead of running this long command, what we can do is we can include this in a local makefile, and we can automate the build instead of typing all these things. That’s why we are going to edit our makefile.

 

To include this command, first, what you do is, you copy make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-  -C  /home/kiran/workspace/ldd/source/linux_bbb_4.14/ M=$PWD modules command shown in Figure 2.

 Cleaning the module
Figure 2. Cleaning the module

 

Prepare Your Working Directory

Ensure your working directory contains only two files: main.c (your kernel module source code) and Makefile.

Your working directory should have only main.c and make file as shown in Figure 3.

Main.c and Makefile
Figure 3. Main.c and Makefile

 

Edit the Makefile

Open your terminal and use your preferred text editor to open the Makefile. In this example, we’ll use gedit

gedit Makefile

After that, let’s edit the makefile now. I would use gedit makefile as shown in Figure 4.

Open Makefile using gedit text editor
Figure 4. Open Makefile using gedit text editor

 

It has got only one entry. Let’s add couple of more entries. Let’s create a target, give a target name as all. all: this is a target. After that, hit enter.

When you press enter, the cursor comes down, Press the tab one time. Here, you have to write the recipe. The recipe is nothing but our command.

Paste the command(make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-  -C  /home/kiran/workspace/ldd/source/linux_bbb_4.14/ M=$PWD modules), what you copied (shown in Figure 5).

recipe command
Figure 5. recipe command

 

Define Variables

Let’s create some variables in the Makefile to make it more readable and maintainable. Replace the existing content with the following:

ARCH=armlets store arm-linux-gnueabihf- value into the CROSS_COMPILE variable, and let’s create one more variable KERN_DIR =

Copy /home/kiran/workspace/ldd/source/linux_bbb_4.14/ as the value for the KERN_DIR (as shown in Figure 6).

Variable creation in Makefile
Figure 6. Variable creation in Makefile

 

 

Let’s fix as shown in Figure 7.

Replacing variables
Figure 7. Replacing variables

ARCH  is equal to arm. We can use this variable name $(ARCH).

This gives me the value of this variable. This will be replaced by arm. Here, instead of arm-linux-gnueabihf- you can use $(CROSS_COMPILE).

And instead of /home/kiran/workspace/ldd/source/linux_bbb_4.14/ Linux kernel source path, place $(KERN_DIR). M is equal to, you have to give the PWD in parenthesis, M=$(PWD), which gives in the path of the current working directory, and this is a target or command modules.

 

After that, let’s create one more target for cleaning. clean:  enter tab. So, you can copy the same command and paste below, and instead of  modules, give clean. You can also create one more command help. You can copy this, paste here and give help. These are the different targets.

For a time being, let’s use only this much, and let’s test this. Let’s save this and exit. 

Let’s just run make as shown in Figure 8.

Running make command
Figure 8. Running make command

You got the result main.ko is formed.

 

If you want to clean it, make clean as shown in Figure 9.

Running make clean
Figure 9. Running make clean

 

You can run make help as in Figure 10. In order to get the help menu of a kernel build system.

Running make help command
Figure 10. Running make help command

 

Let’s again edit makefile to add one more target to build against host instead of target. You can use the same command, but you have to edit.

How to create Makefile
Figure 11. Target to build against host

Remove ARCH=arm ,cross compilation,.KERN directory, let’s use the word HOST_KERN_DIR and let me do modules(as shown in Figure 11).

And now let’s define this variable is equal to /lib/modules/  you have to use the name. So, here you have to mention the kernel version of your running Linux kernel of your host.

You can a manually type it or you can use a uname -r. Just do $(shelluname -r) and /build/, Or you can run uname -r on your command prompt get the version string and paste here. Let’s save this and let’s exit.

 

Let’s make clean first. Let’s make host as shown in the Figure 12.

How to create Makefile
Figure 12. Run make host

 

You can see that the kernel module has been built for the host. You can insert that. And if you want to build for target, then you just have to run the command make. This actually built the kernel module for the target.

How to create Makefile
Figure 13. Running make and make host command

If you run a file main.ko, it shows that it is for arm. Whereas, if you run make host, it shows different( as shown in Figure 13).

For example, let’s again run file main.ko. You can see that it is for x86. This is about creating a small makefile to automate our kernel building procedures.

In the following article, let’s explore In-tree building.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.