# 'No rule to make target' when compiling a Linux kernel module

**URL:** https://discourse.cmake.org/t/no-rule-to-make-target-when-compiling-a-linux-kernel-module/13106
**Category:** Usage
**Tags:** os:linux
**Created:** [December 2, 2024, 11:26am UTC](https://discourse.cmake.org/t/no-rule-to-make-target-when-compiling-a-linux-kernel-module/13106 "2024-12-02T11:26:43Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![CloseHandle](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/898d66/32.png) [@CloseHandle](https://discourse.cmake.org/u/CloseHandle)
#### Post date: [December 2, 2024, 11:26am UTC](https://discourse.cmake.org/t/no-rule-to-make-target-when-compiling-a-linux-kernel-module/13106/1 "2024-12-02T11:26:43Z")

</div>

Hello,

I failed to compile a Linux kernel module using CMake, for example, “Hello World” projects in the following repositories:

1. [Christoph Acham / cmake-kernel-module · GitLab](https://gitlab.com/christophacham/cmake-kernel-module)
2. [GitHub - enginning/cmake-kernel-module: cmake-kernel-module for clion](https://github.com/enginning/cmake-kernel-module)

The important part of the `CMakeLists.txt` file is as follows:

```auto
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:

```sh
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:

```sh
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?
