Hello,
I failed to compile a Linux kernel module using CMake, for example, “Hello World” projects in the following repositories:
- Christoph Acham / cmake-kernel-module · GitLab
- GitHub - enginning/cmake-kernel-module: cmake-kernel-module for clion
The important part of the CMakeLists.txt
file is as follows:
set(DRIVER_FILE hello.ko)
set(KBUILD_CMD $(MAKE) -C ${KERNELHEADERS_DIR} modules M=${CMAKE_CURRENT_BINARY_DIR} src=${CMAKE_CURRENT_SOURCE_DIR})
FILE(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/Kbuild "obj-m := hello.o")
add_custom_command(OUTPUT ${DRIVER_FILE}
COMMAND ${KBUILD_CMD}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS hello.c VERBATIM)
add_custom_target(driver ALL DEPENDS ${DRIVER_FILE})
and the error I get is shown below:
user@machine:~/cmake-kernel-module/build$ make driver
[100%] Generating hello.ko
make[6]: *** No rule to make target '/home/user/cmake-kernel-module/build/hello.o', needed by '/home/user/cmake-kernel-module/build/'. Stop.
make[5]: *** [/usr/src/kernels/6.11.10-300.fc41.x86_64/Makefile:1966: /home/user/cmake-kernel-module/build] Error 2
make[4]: *** [Makefile:236: __sub-make] Error 2
make[3]: *** [CMakeFiles/driver.dir/build.make:73: hello.ko] Error 2
make[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/driver.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/driver.dir/rule] Error 2
make: *** [Makefile:124: driver] Error 2
I tried changing KBUILD_CMD
and WORKING_DIRECTORY
, however, could not make it work.
The module compiles without any error using Makefile, for example:
obj-m := hello.o
all:
make -C /usr/lib/modules/$(shell uname -r)/build M=$(PWD) modules
I am using CMake 3.30.5 on Fedora 41, and GNU Make 4.4.1.
Can you please help me fix the issue?