I'm trying to build a simple kernel module for arm64 machine from my x86_64 machine. The target and host are both using ubuntu 20.04 as OS. First I tried building a simple kernel module for the host (x86_64) using this Makefile. By the way, uname -r
gives 5.4.0-77-generic in my case.
obj-m += chr_drv_ex1.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
The program builds and runs ok.
Now I want to try it for arm64 ubuntu 20.04 machine target (I have a virtual machine). One idea is to copy the /lib/modules/5.4.0-77-generic/build directory from the arm64 machine and use the folder as the -C option for my cross-compile. But I soon found that this 'build' directory a symbolic link to /usr/src/linux-headers-5.4.0-77-generic and many files or directories there are also symbolic links to those under /usr/src/linux-headers-5.4.0-77. So I need to copy the /lib/modules/5.4.0-77-generic, /usr/src/linux-headers-5.4.0-77 and /usr/src/linx-headers-5.4.0-77-generic. And this doesn't seem nice.
So my question is: how should I provide the -C option (where build scripts are located) in this case? Can I just install linux-headers-5.4.0-77 and linux-headers-5.4.0-77-generic in my host system and use them? (I tried it but I have this compile error:
make ARCH=arm64 CROSS_COMPILE=aarch64-none-linux-gnu- -C /lib/modules/5.4.0-77-generic/build M=/home/ckim/pprj/qemu_test/test_ldd modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-77-generic'
CC [M] /home/ckim/pprj/qemu_test/test_ldd/chr_drv_ex1.o
In file included from ./include/linux/types.h:6,
from ./include/linux/limits.h:6,
from ./include/linux/kernel.h:7,
from /home/ckim/pprj/qemu_test/test_ldd/chr_drv_ex1.c:1:
./include/uapi/linux/types.h:5:10: fatal error: asm/types.h: No such file or directory
5 | #include <asm/types.h>
| ^~~~~~~~~~~~~
compilation terminated.
make[2]: *** [scripts/Makefile.build:271: /home/ckim/pprj/qemu_test/test_ldd/chr_drv_ex1.o] Error 1
make[1]: *** [Makefile:1762: /home/ckim/pprj/qemu_test/test_ldd] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-77-generic'
make: *** [Makefile:4: all] Error 2