Makefile only works on terminal, fails when using Emacs - c

I am currently trying to write basic kernel module on linux. I can compile the source code on terminal without any error
~/Desktop/kernelDriver$ make
make -C /lib/modules/5.8.0-7642-generic/build M=/home/cryonayes/Desktop/kernelDriver modules
make[1]: Entering directory '/usr/src/linux-headers-5.8.0-7642-generic'
CC [M] /home/cryonayes/Desktop/kernelDriver/basicModule.o
MODPOST /home/cryonayes/Desktop/kernelDriver/Module.symvers
CC [M] /home/cryonayes/Desktop/kernelDriver/basicModule.mod.o
LD [M] /home/cryonayes/Desktop/kernelDriver/basicModule.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.8.0-7642-generic'
But when I try to compile with same files on Emacs I get this error
-*- mode: compilation; default-directory: "~/Desktop/kernelDriver/" -*-
Compilation started at Wed Mar 3 20:42:07
make
make -C /lib/modules/5.8.0-7642-generic/build M=/home/cryonayes/Desktop/kernelDriver modules
make[1]: *** /lib/modules/5.8.0-7642-generic/build: No such file or directory. Stop.
make: *** [Makefile:7: all] Error 2
Compilation exited abnormally with code 2 at Wed Mar 3 20:42:07
It says No such file or directory. but it actually exists.
Here is the content of my Makefile
obj-m += basicModule.o
KERNEL_DIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean
What causes this error ?

As far as I understand, the /lib/modules/5.8.0-7642-generic/build is a symlink to /usr/src/linux-headers-5.8.0-7642-generic.
You can use /usr/src/linux-headers-$(shell uname -r) instead and try again.
Add a test rule to show information about -C's state as follows;
test:
stat $(KERNEL_DIR)
This should show what and where it's pointing to.
If you're in GUI session, something might have sandboxed your emacs while terminal is running without it. You may have some kind of "fake-root" to isolate it from vulnerabilities.

Related

Ubuntu compiling kernel module first time

I'm trying to compile a simple kernel module for the first time:
#include <linux/module.h>
#include <linux/kernel.h>
int init_nodule(void)
{
printk(KERN_INFO "Hello world1.\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye\n");
}
I've used obj-m += hello-1.o (that's the name of the module) but i'm getting an error:
obj−m: command not found
Why is this happening? I tried looking online for a solution, but nothing I found helped..
EDIT: After modifying based on #Mathieu answer , I get the following error :
> make -C /lib/modules/4.18.0-15-generic/build M=/home/galco modules
make[1]: Entering directory '/usr/src/linux-headers-4.18.0-15-generic'
Makefile:970: "Cannot use CONFIG_STACK_VALIDATION=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel"
scripts/Makefile.build:45: /home/galco/Makefile: No such file or directory
make[2]: *** No rule to make target '/home/galco/Makefile'. Stop.
Makefile:1534: recipe for target '_module_/home/galco' failed
make[1]: *** [_module_/home/galco] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.18.0-15-generic'
makefile:4: recipe for target 'all' failed
make: *** [all] Error 2
The line obj-m += hello-1.o must be put in a file named Makefile
So it will looks like:
obj-m += hello-1.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
To launch the build process, just execute make from your command line.
More resource: https://qnaplus.com/how-to-compile-linux-kernel-module/

GCC Kernel Module Compilation Errors

I'm working on a netfilter and compiling it on a virtual machine.
matt#ubuntu:~$ make
gcc -c -O2 -W -isystem /lib/modules/4.4.0-87-generic/build/include -D__KERNEL__ -DMODULE test10.c -I.
In file included from /usr/src/linux-headers-4.4.0-87/include/linux/kernel.h:6:0,
from structs1.h:2,
from test10.c:1:
/usr/src/linux-headers-4.4.0-87/include/linux/linkage.h:7:25: fatal error: asm/linkage.h: No such file or directory
compilation terminated.
makefile:2: recipe for target 'test' failed
make: *** [test] Error 1
Above is my GCC command used to attempt to build my kernel module and the subsequent error that it throws.
In researching this, I have found one possible solution that involves specifiying the kernel version as such:
KERNEL_VER=/usr/src/linux-headers-4.4.0-87/arch/x86/
But two problems:
I'm not sure how to actually use this in my make file which can be seen below, outside of just making a symbolic link, and
I looked in this folder (/usr/src/linux-headers-4.4.0-87/arch/x86/) and sub folders and it doesn't have any of the same kernel.h files -- which is what I need.
Makefile:
test:
gcc -c -O2 -W -isystem /lib/modules/4.4.0-87-generic/build/include -D__KERNEL__ -DMODULE test10.c -I.
Any help on this would be greatly appreciated.
The standard Makefile used to build a loadable kernel module is as follows.
obj-m += test10.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
Refer Compiling a loadable kernel module this for more information.

compile kernel module error

I wish to run this kernel module
code file hello.c
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void) {
printk(KERN_INFO "Hello world!\n");
return 0;
}
void cleanup_module(void) {
printk(KERN_INFO "Goodbye world!\n");
}
i ran the makefile below
obj-m += hello.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
but I am getting the following errors
make -C /lib/modules/4.4.0-45-generic/build M=/home/fyousry/Desktop/Untitled Folder 4 modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-45-generic'
arch/x86/Makefile:148: CONFIG_X86_X32 enabled but no binutils support
Makefile:676: Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: -fstack-protector-strong not supported by compiler
make[1]: *** No rule to make target 'Folder'. Stop.
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-45-generic'
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 2
the directory which Contain this code has space
when i removed space it is work (UntitledFolder4 instead of Untitled Folder 4)
You should use double quotes when you give a path to a command, especially if the path is contain a sub-command or a variable.
Example:
make -C "/lib/modules/$(shell uname -r)/build" "M=$(PWD)" modules
You can also escape the space if you want to keep it spaced :
make -C /lib/modules/4.4.0-45-generic/build M=/home/fyousry/Desktop/Untitled\ Folder\ 4 modules

Build Linux Kernel Module using Makefile with different pathname

I am trying to compile a Linux kernel module using the standard example Makefile specified in the Linux Kernel Module Programming Guide. If the Makefile is called Makefile, then everything works. If I rename the Makefile to Makefile.hello or something else, then it fails as it cannot find the path Makefile. I was wondering if there is a command or set of flags I can add to my Makefile to make this function properly. I need to rename the Makefile as I am calling it from CMake. Cmake creates its own Makefiles and will commonly overwrite what I already have.
I replaced my kernel module code with the hello world example and replicated the problem. I know its the makefile.
hello world example hello.c
/*
* hello−1.c − The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
Makefile
obj-m += hello.o
ifeq (,$(KDIR))
KDIR := /lib/modules/$(shell uname -r)/build
endif
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) $(KCONFIG) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
If makefile is called Makefile. (Successfully builds)
$> make -f Makefile
make -C /lib/modules/4.4.0-21-generic/build M=/home/msmith/Desktop/kernel-test modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-21-generic'
CC [M] /home/msmith/Desktop/kernel-test/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/msmith/Desktop/kernel-test/hello.mod.o
LD [M] /home/msmith/Desktop/kernel-test/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-21-generic'
Make output if makefile is called Makefile.hello (Fails to build)
$> make -f Makefile.hello
make -C /lib/modules/4.4.0-21-generic/build M=/home/msmith/Desktop/kernel-test modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-21-generic'
scripts/Makefile.build:44: /home/msmith/Desktop/kernel-test/Makefile: No such file or directory
make[2]: *** No rule to make target '/home/msmith/Desktop/kernel-test/Makefile'. Stop.
Makefile:1396: recipe for target '_module_/home/msmith/Desktop/kernel-test' failed
make[1]: *** [_module_/home/msmith/Desktop/kernel-test] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-21-generic'
Makefile.hello:10: recipe for target 'all' failed
make: *** [all] Error 2
I tried adding the -f to the internal MAKE parameters, however that just caused more issues.
Move all Kbuild-related logic into the file Kbuild. Kernel's build system checks file with this name first, so it won't look into Makefile, created by CMake. This feature is documented in Documentation/kbuild/makefiles.txt.
I use exactly this approach in my CMake projects, related with Linux kernel.
Open script/Makefile.build into kernel tree:
41 # The filename Kbuild has precedence over Makefile
42 kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
43 kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile)
44 include $(kbuild-file)
This part of code (43-44) include Makefile with name 'Makefile'.
The default Makefile. Name = Makefile ... and Makefile-hello, or Makefile.hello.
obj-m := hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
$ make : OK
$ make -f Makefile-hello OK
$ make -f Makefile.hello Also OK.
Your Makefile : $ make -f Makefile.msmith OK, no errors.

makefile for a libusb program

I have made a C program using libusb and I am using following command to compile it:
gcc -o usbtest.o usbtest.c -lusb-1.0
The program is working fine. Next, I added the code of "usbtest.c" to kernel module (usbmod.c) and I am stuck with the make file. I am not sure what command I should pass in the "all" section. Here is what I have made:
obj-m := usbmod.o
KERNEL_DIR = /lib/modules/$(shell uname -r)/build
PWD = $(shell pwd)
all:
$(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.o *.ko *.mod.* *.symvers *.order *-
After running make, I am getting the following error:
anubhav#anubhav-Inspiron-3421:~/Desktop/usb$ make
make -C /lib/modules/3.13.0-46-generic/build SUBDIRS=/home/anubhav/Desktop/usb modules
make[1]: Entering directory `/usr/src/linux-headers-3.13.0-46-generic'
CC [M] /home/anubhav/Desktop/usb/usbmod.o
/home/anubhav/Desktop/usb/usbmod.c:3:19: fatal error: stdio.h: No such file or directory
#include <stdio.h>
^
compilation terminated.
make[2]: *** [/home/anubhav/Desktop/usb/usbmod.o] Error 1
make[1]: *** [_module_/home/anubhav/Desktop/usb] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-46-generic'
make: *** [all] Error 2
Not sure if need to bring any header file to my working directory or what. Kindly provide suggestions.
Okay, so this link clarifies my doubts to some extent:
[][1]error: stdio.h: No such file or directory error during make
It says "stdio.h" and all do not exist in kernel space and therefore such errors arise. Besides, obviously my module does not contain any printf so I suppose I don't need "stdio.h".
But it does use libusb extensively. So, is there a way to really create this module.

Resources