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:
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.
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.
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.
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).
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=arm, lets 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).
Let’s fix as shown in Figure 7.
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.
You got the result main.ko is formed.
If you want to clean it, make clean as shown in Figure 9.
You can run make help as in Figure 10. In order to get the help menu of a kernel build system.
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.
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.
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.
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