Simple ubuntu kernel module make file error - c

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! :)

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.

Related

MakeFile is not found when I call make

I have a small C program that just computes Fibonacci. I have make file to build the file, and when I call make, I get the message make: *** No targets specified and no makefile found. Stop.. If I call make clean, I get make: *** No rule to make target `clean'. Stop. but it seems to see a makeFile (I think). I'm pretty lost and need help.
Here's the text of the make file:
CC=gcc
all: fibonacci
fibonacci: fibonacci.c
$(CC) -pthread -o fib.exe
clean:
rm fib.e xe
rename your makefile to Makefile or use make -f <whatever_name_you_like>. Remember that in unix-like systems file names are often case-sensitive (not in all types of filesystems but in many)
Refer this answer No targets specified and no makefile found
By default, when make looks for the makefile, it tries the following names, in order: GNUmakefile, makefile and Makefile.
You can also try make all and read
What Name to Give Your Makefile

make: *** No rule to make target `clean'. Stop

I was walking through this tutorial, where I had to create this make file:
CFLAGS=-wall -g
clean:
rm -f ex1
When I type this command:
$ make clean
I get the following error:
make: *** No rule to make target `clean'. Stop.
I made sure that I'm using TABS. Why am I getting this error? How can I solve it?
The issue here, as indicated by a few comments on the post is that you named your file make instead of the traditional Makefile (or alternate names GNUMakefile and makefile that GNU make supports).
See What Name to Give Your Makefile in the GNU make manual.
If you want to use an alternate name (like make) then you need to tell make to use that file with the -f flag (also mentioned in that section of the manual).
make -f make
The missing separator error is caused by incorrect indentation in the makefile. Spaces instead of tabs on recipe lines, etc.
I was under the impression that targets should not have whitespace in front of them.
Also, Make treats the first target as the default target (what we traditionally call 'all'), so if 'clean' is your only target, it will get treated as the default.
I'm guessing your indentation is borked. Make is very, very specific in how files should be indented.
Your file should look like this:
CFLAGS=-wall -g
clean:
rm -f ex1
Notice how clean is all the way left in the file (has no whitespace in front of it), and how there are two tabs (represented here by 8 spaces because I have no way of representing tabs on SO...)
basically, Make is an ancient program that has very, VERY particular rules as to how things are done. Be careful and follow the indent rules as closely as you possibly can.

Cross Compile Kernel Module for 4.1.2 can't find plat/dmtimer.h

I am using a Beaglebone Black to try and learn how to write kernel modules. I wanted to start with something somewhat simple, so thought writing a module to control a hardware timer and blink an LED would be a relatively simple thing to accomplish. I have downloaded the 4.1.2-bone12 source, cross compiled it, and created an SD card to boot from by following a guide written by Robert C Nelson.
I have written a .c file and Makefile for the module. The Makefile is as follows:
ARCH=arm
COMPILER_LOC=/home/tom/ti/Kernel/bb-kernel-4.1.2-bone12/dl/gcc-linaro-4.9-2015.02-3-x86_64_arm-linux-gnueabihf/bin
COMPILER=$(COMPILER_LOC)/arm-linux-gnueabihf-
KDIR := /home/tom/ti/Kernel/bb-kernel-4.1.2-bone12/KERNEL
obj-m := timer.o
default:
$(MAKE) -C $(KDIR) M=$$PWD ARCH=$(ARCH) CROSS_COMPILE=$(COMPILER) modules
clean:
$(MAKE) -C $(KDIR) M=$$PWD ARCH=$(ARCH) clean
When I run make it throws the error:
make[1]: Entering directory `/home/tom/ti/Kernel/bb-kernel-4.1.2-bone12/KERNEL'
CC [M] /home/tom/ti/Kernel/Timer/timer.o
/home/tom/ti/Kernel/Timer/timer.c:5:26: fatal error: plat/dmtimer.h: No such file or directory
#include <plat/dmtimer.h>
^
compilation terminated.
make[2]: *** [/home/tom/ti/Kernel/Timer/timer.o] Error 1
make[1]: *** [_module_/home/tom/ti/Kernel/Timer] Error 2
make[1]: Leaving directory `/home/tom/ti/Kernel/bb-kernel-4.1.2-bone12/KERNEL'
make: *** [default] Error 2
Originally I was using the 3.8.13-bone72 kernel and was not having this issue. I only made 2 changes in the Makefile, the .c did not change when going from the 3.8.13-bone72 kernel to 4.1.2-bone12. The two changes are:
COMPILER_LOC=/home/tom/ti/Kernel/bb-kernel-3.8.13-bone72/dl/gcc-linaro-arm-linux-gnueabihf-4.7-2013.04-20130415_linux/bin
KDIR := /home/tom/ti/Kernel/bb-kernel-3.8.13-bone72/KERNEL
Both the 3.8.13-bone72 and 4.1.2-bone12 sources have the dmtimer.h located in
../KERNEL/arch/arm/plat-omap/include/plat/.
So my question, why would cross compiling with the 4.1.2-bone12 kernel fail when it succeeded with the 3.8.13-bone72?
Also, for people wondering the reason I switched from the 3.8.13-bone72 to 4.1.2-bone12 is because the 3.8.13-bone72 does not appear to allow you to specific which hardware timer to use.. The 4.1.2-bone12 allows a specific timer to be used.
EDIT - 7/21/2015
I have uploaded the debug outputs from a make -d when building the module for both the 3.8.13-bone72 and 4.1.2-bone12 kernels. There is a difference between the two; it seems to my inexperienced eye that when building against the 4.1.2 kernel it is not switching directories to the kernel source.
If you look at the Debug3.8.txt, you see that after the second "PARTICULAR PURPOSE." line it begins doing things in /home/tom/ti/Kernel/bb-kernel-3.8.13-bone72/KERNEL and subdirectories. This does not appear to happen when building against the 4.1.2 kernel though.
LINK: https://onedrive.live.com/redir?resid=C46E7B4F8242576!1497&authkey=!AO2GkaEOw41SVC8&ithint=folder%2ctxt

Using CMake with AVR Toolchain in Cygwin or MinGW

I'm currently trying to get a toolchain setup so I can build an AVR project from CLion.
My starting point is this, specifically, the Blink example. The issue is that it, along with existing CMake for AVR examples, are all for Linux based systems.
What I've tried is installing WinAVR to get the executables. I've modified the CMakeList.txt so the program names contain the following:
set(AVRCPP "C:/WinAVR-20100110/bin/avr-g++")
set(AVRC "C:/WinAVR-20100110/bin/avr-gcc")
set(AVRSTRIP "C:/WinAVR-20100110/bin/avr-strip")
set(OBJCOPY "C:/WinAVR-20100110/bin/avr-objcopy")
set(OBJDUMP "C:/WinAVR-20100110/bin/avr-objdump")
set(AVRSIZE "C:/WinAVR-20100110/bin/avr-size")
set(AVRDUDE "C:/WinAVR-20100110/bin/avrdude")
set(AVRAS "C:/WinAVR-20100110/bin/avr-as")
While using the Cygwin environment, CMake has no issue finding my compilers, but when I try to build the project, avr-gcc is being passed parameters in Linux format.
C:/WinAVR-20100110/bin/avr-gcc.exe -o CMakeFiles/cmTryCompileExec420260872.dir/testCCompiler.c.obj -c /cygdrive/c/Users/Daniel/.clion10/system/cmake/generated/2eb381d5/2eb381d5/__default__/CMakeFiles/CMakeTmp/testCCompiler.c
avr-gcc.exe: /cygdrive/c/Users/Daniel/.clion10/system/cmake/generated/2eb381d5/2eb381d5/__default__/CMakeFiles/CMakeTmp/testCCompiler.c: No such file or directory
Is there a way to have CMake pass avr-gcc arguments in a format it can work with?
For reference, this is the full output:
Error:The C compiler "C:/WinAVR-20100110/bin/avr-gcc" is not able to compile a simple test program.
It fails with the following output:
Change Dir: /cygdrive/c/Users/Daniel/.clion10/system/cmake/generated/2eb381d5/2eb381d5/__default__/CMakeFiles/CMakeTmp
Run Build Command:/usr/bin/make.exe "cmTryCompileExec420260872/fast"
/usr/bin/make -f CMakeFiles/cmTryCompileExec420260872.dir/build.make CMakeFiles/cmTryCompileExec420260872.dir/build
make[1]: Entering directory '/cygdrive/c/Users/Daniel/.clion10/system/cmake/generated/2eb381d5/2eb381d5/__default__/CMakeFiles/CMakeTmp'
/usr/bin/cmake.exe -E cmake_progress_report /cygdrive/c/Users/Daniel/.clion10/system/cmake/generated/2eb381d5/2eb381d5/__default__/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec420260872.dir/testCCompiler.c.obj
C:/WinAVR-20100110/bin/avr-gcc.exe -o CMakeFiles/cmTryCompileExec420260872.dir/testCCompiler.c.obj -c /cygdrive/c/Users/Daniel/.clion10/system/cmake/generated/2eb381d5/2eb381d5/__default__/CMakeFiles/CMakeTmp/testCCompiler.c
avr-gcc.exe: /cygdrive/c/Users/Daniel/.clion10/system/cmake/generated/2eb381d5/2eb381d5/__default__/CMakeFiles/CMakeTmp/testCCompiler.c: No such file or directory
avr-gcc.exe: no input files
CMakeFiles/cmTryCompileExec420260872.dir/build.make:60: recipe for target 'CMakeFiles/cmTryCompileExec420260872.dir/testCCompiler.c.obj' failed
make[1]: Leaving directory '/cygdrive/c/Users/Daniel/.clion10/system/cmake/generated/2eb381d5/2eb381d5/__default__/CMakeFiles/CMakeTmp'
make[1]: *** [CMakeFiles/cmTryCompileExec420260872.dir/testCCompiler.c.obj] Error 1
Makefile:117: recipe for target 'cmTryCompileExec420260872/fast' failed
make: *** [cmTryCompileExec420260872/fast] Error 2
CMake will not be able to correctly generate this project.
I use cmake and avr on windows and on linux.
The syntax is the same. Why do you want to use cygwin in the mid of that?
In any case you didn't show your toolchain file.
When cross compiling using cmake you need to provide a toolchain file where you set all the configuration related to the compiler.
You need to do this because when cmake starts it try to compile a simple program and it try to run it. If you are using an avr compiler on a computer cmake can't run the executable, so it fails.
You need to put an extra care including this command in the toolchain:
SET(CMAKE_SYSTEM_NAME Generic)
it is needed for skip this compilation and so to avoid the failure.
I think this is a good read where to begin:
http://playground.arduino.cc/Code/CmakeBuild

Compiling Small Gcc Project on Windows Using MinGW

so I've been programming in C++ for almost 2 years now, and the whole while I've had the pleasure of using an IDE (VS) with lovely project settings and automatic linking and the like. I've always stayed away from any external libraries which required me to compile via makefiles, or at least the ones which were meant for linux environments/other compilers.
Anyways I now want to use a super handy utility (Bob Jenkins Perfect Minimal Hash) but it requires me to compile via makefiles, not only that but using the g++ compiler.
I went ahead and got the mingW32-make utility and am now trying to get it to work. Where I'm at now:
Succesfully installed minGW
Succesfully called the make utility
Failed to succesfully make the project.
The error I get is:
C:\gen_progs\ph>mingw32-make
mingw32-make: *** No rule to make
target lookupa.c', needed by lookupa.o'. Stop.
And the makefile itself:
CFLAGS = -O
.cc.o:
gcc $(CFLAGS) -c $<
O = lookupa.o recycle.o perfhex.o perfect.o
const64 : $(O)
gcc -o perfect $(O) -lm
# DEPENDENCIES
lookupa.o : lookupa.c standard.h lookupa.h
recycle.o : recycle.c standard.h recycle.h
perfhex.o : perfhex.c standard.h lookupa.h recycle.h perfect.h
perfect.o : perfect.c standard.h lookupa.h recycle.h perfect.h
Now the error seems reasonable, at least from my minimal understanding of makefiles, I have all the referenced .c, .h files, however I have none of the .o files and there doesn't appear to be any instructions on how to make these. So my question/s are:
am I calling the make utility wrong? Or do I need to compile the object files first? Or... do I need to add something to the make file?
Again I have all the referenced .c and .h files.
Edit: Sorry about that I was actually missing that specific file it seems to have disapeared somewhere along the line. However, adding it back in this is the error I now get:
c:\gen_progs\ph>mingw32-make
cc -O -c -o lookupa.o lookupa.c
process_begin: CreateProcess(NULL, cc -O -c -o lookupa.o lookupa.c, ...) failed.
make (e=2): The system cannot find the file specified.
mingw32-make: *** [lookupa.o] Error 2
Regarding your error "process_begin: CreateProcess(NULL, cc -O -c -o lookupa.o lookupa.c, ...) failed."
This is because the make utility wants to use the "cc" compiler to compile your program, but that compiler is not part of the Mingw-package.
Solution: Change the ".cc.o:" to ".c.o:". This changes the implicit rule which tells Make what compiler to use (gcc on the next line) when compiling .c files (the original line tells it how to compile .cc files).
Saying either make -DCC=gcc at the command line or adding the line CC=gcc to the top of the Makefile would cure the issue as well. Make's built in rules for handling C source code all name the C compiler with the variable CC, which defaults to "cc" for reasons of backward compatibility even in Gnu Make.
It looks like the original Makefile author tried to work around that problem by supplying a custom rule for compiling .cc files, but since there are no .cc files in the project that rule was not actually used.
Specifying the correct value for CC is superior to fixing the explicit rule to name .c files IMHO because Makefiles are generally easier to use and maintain and are the most portable when the least possible information is specified.
I don't think not having .o files is the problem. Make will make them from the source files (the files to the right of the colon).
Your immediate problem seems to be that make can't file the file "lookupa.c". From the rules you posted, it looks to me like that file should be sitting in the same directory as the makefile, but it isn't. You need to figure out where that file is, and how to get it there.
(For some reason I have a mental image of Wile E. Coyote sitting at his computer, seeing that file name, looking up, and getting plastered with an anvil).

Resources