I am trying to build a very simple kernel module but I get met with this error:
make -C /lib/modules/5.16.14-051614-generic/build M=/home/nanyo/Documents /ProgrammingEnvs/LinuxKernelDriver modules
make[1]: Entering directory '/usr/src/linux-headers-5.16.14-051614-generic'
make[2]: *** No rule to make target '/home/nanyo/Documents/ProgrammingEnvs/LinuxKernelDriver/hi.o', needed by '/home/nanyo/Documents/ProgrammingEnvs/LinuxKernelDriver/hi.mod'. Stop.
make[1]: *** [Makefile:1852: /home/nanyo/Documents/ProgrammingEnvs/LinuxKernelDriver] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.16.14-051614-generic'
make: *** [Makefile:4: all] Error 2
Here is the Makefile:
obj-m = hi.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(CURDIR) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(CURDIR) clean
Although there are similar errors on stack overflow none of them spcifically say no rule to target '.../name.o' needed by '.../name.mod'
I run make like so ~/Documents/ProgrammingEnvs/LinuxKernelDriver$ make
. Attempting any of the proposed solutions from other questions with sudo and experimenting with CURDIR and PWD did not help.
I am running on Ubuntu 22.04 jammy, wiht the 5.16.14 kernel (I have to run this kernel as older ones dont seem to work well with my VERY recent hardware)
I have been trying to compile 15 lines of code for 2 days now, HELP! :)
CodePudding user response:
If you ask make
to build an object file like hi.o
, it will look to find a source file to build the object file from. It won't look for any random source file (what if you had 20 source files? How should it figure out which one you meant?), it will look for a source file based on the name of the object file.
So if you want to compile C code into hi.o
, make will look for a source file named hi.c
. If you want to compile C code into hi.o
, make will look for a source file like hi.cpp
. If you want to compile Fortran, make will look for hi.f
, etc.
Note that Linux systems, unlike Windows and MacOS, use case-sensitive filenames so hi.c
, Hi.c
, HI.c
, etc. are all DIFFERENT files, and hi.o
will only match the first one.
If make can't find a source file related to the name of the object file, it's not going to say "well, there's just one source file here, that must be what they meant". That kind of arbitrary behavior is a bad thing in a build tool: make does exactly what you tell it to do, and if it can't it doesn't try to guess what you meant. It gives you an error, like no rule to make target 'hi.o'.
If you wanted to use a different object and source name for some reason you CAN do it, but you have to tell make about it by writing your own explicit rule:
hi.o : mycoolfile.c
<recipe to build hi.o from mycoolfile.c>
Since there's no way the kernel build can know what source file name you might choose, you'll have to write these rules in your own makefile.