Problem with Makefile compilation on Ubuntu, -lhidapi-libusb library - c

I have a slight problem with a Linux module compilation. No matter where I put a -lhidapi-libusb library reference in the make command, the module just refuses to compile. I know I'm doing something wrong, please help me, if you have some time.
Thanks
obj-m += light.o
all:
make -lhidapi -libusb -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -lhidapi -libusb -C /lib/modules/$(shell uname -r)/build M=$(PWD)
Output:
fatal error: hidapi/hidapi.h: No such file or directory
#include <hidapi/hidapi.h>

What you're trying to do doesn't make sense, and will not work.
libusb and HIDAPI are userspace libraries. They cannot be used within a kernel module.

You need to actually install the hidapi header library:
$ sudo apt install libhidapi-dev
https://github.com/libusb/hidapi#installing-hidapi

Related

What do I need to point my makefile at to make it build a kernel driver properly?

I am working on a simple driver for a class. I am using this makefile:
obj-m := mysourcefile.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/kernel
all default: modules
install: modules_install
modules modules_install help clean:
$(MAKE) -C $(KERNELDIR) M=$(pwd) $#
When I run this using the line:
make
I get the error:
no rule to make target 'modules'. Stop.
I do want to note, my textbook suggests this line:
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
However, I have no "build" directory in my version of Debian (10.6.0-3) (Kernel v. 4.19.0-10-amd64), so I changed the file to point to kernel, since I believe the makefile is supposed to point to the kernel. I don't know if this was the correct choice or not. My active directory IS the directory which contains my source code.
I am unsure of how to proceed from this point.

how to compile a kernel module

I'm trying to compile a simple hello world module following this guide and I'm confused about what the Makefile is actually doing.
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
I understand that when i type the make command it will run the all recipe which runs make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules. So now it runs the Makefile found at the path given after the -C flag but what does the M=$(PWD) modules do?
'obj-m' :- specifies object files which are built as loadable
kernel modules.
'all and clean' :- If you run 'make' by default it will run "all :".
But we can use all and clean with make. it will run only those specific command.
Example :-
'make all' will run "make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules"
'make clean will run "make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean"
3.'uname -r' :- get name and information about current kernel.
Example :- for me, my kernel is "4.6.0-rc1".
Option ‘-C dir’ :- Change to directory dir before reading the makefiles.
Example :- "make -C /lib/modules/$(shell uname -r)/build" will change to "make -C /lib/modules/4.6.0-rc1/build.
'$pwd':- get the path of your current directory.
Now you want to create your loadable module by using "make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules".
Your source code need environment to run. That's why we have to use -C option to change build directory. Which have all needed definition, header file, Macro and etc. Now after changing to build directory you need to tell where is your module present, that's why we are using M=$PWD.
To compile kernel module, the make command you normally need is in this form:
make -C /lib/modules/3.16.0-70-generic/build M=/home/test/ldd3/hello modules
in which, -C means switching to another path.
/lib/modules/3.16.0-70-generic/
is the path to the kernel in used, and
/home/test/ldd3/hello
is where the source of your module resides.
what does the M=$(PWD) modules do?
So as I said M=$(PWD) is simply a shell variable that stores the current path to your kernel module. make needs to store this as it switches to the kernel build path.

error in loadable kernel module as make: nothing to be done for all?

The functions you write to provide procfs interfaces is just code that is part of your LKM source.
http://linux.die.net/lkmpg/x769.html has a simple example using procfs, reproduced here:
I copied the code from above link -
You'll find a tutorial for building kernel modules at http://www.cyberciti.biz/tips/compiling-linux-kernel-module.html. The summary of that is:
1) Ensure you have kernel source installed in /usr/src.
2) Create a makefile that looks like:
obj-m = procfs2.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
3) build the module with the command make 4) load the module into memory with the command insmod procfs2.ko (do this as the root user)
I copied the code and created the MAKEFILE and later if I give the make command from the console then it is showing as : make: nothing to be done for all. could someone please tell me what could be the error ??
Here is an example of a Makefile for a kernel module.
the important thing here to note is that the dots shown below must be replaced by a TAB, replacing them by spaces will cause Makefile to malfunction.
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

Adding userspace header files to make file

I am building a linux device using make and i need to use string.h in my device. I tried to add /usr/include to make file but it does not work. can any one help me on adding another include path to make file.
my make file is
KBUILD_CFLAGS += -w
obj-m += netlinkKernel.o
all:
make -w -C /lib/modules/$(shell uname -r)/build CPPFLAGS="usr/include/" M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
That's because just usr/include/ isn't a proper compiler flag. You need to use e.g. -I/usr/include/.
However, using libraries from userspace in a kernel driver might not work as you expect it to, like not at all. The kernel should have have it's own string library (including the "string.h" header file) that you should use.
it seems like we can not use standard header files in kernel programming. Thanks for your help

Linux kernel module compiling

I try to compile simple linux kernel module:
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk("Hello world 1.\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_ALERT "Goodbye world 1.\n");
}
My makefile:
obj-m = testmodule.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
Now i haven't errors in my .c file.
But when i try make in terminal: make: Nothing to be done for `all'.
What's wrong?
Thank you.
The default command in your makefile is
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
That instructs make to cd to /lib/modules/$(KVERSION)/build and run
make module m=YOUR_CURRENT_DIR
In turn, that makefile is not finding anything to do. Presumably, that makefile expects to find some particular structure in your current directory.
You need to more carefully read whatever instructions led you to set up this makefile.
You really don't give enough information to determine the correct answer, but I'll give you some things to look into.
How are you compiling it? Do you have the correct include path in your build? Build paths are specified with the -I option to gcc. Make sure you are pointing to your kernel source tree.
Have you build the kernel? When you do a make, certain things are setup that allows you to build. Doing a make doesn't build everything (like modules) but will get the initial stuff setup for you.
Make sure your have your distributions the kernel header/development packages installed that provide these include files.
If they are installed, search where these include files are located on your computer and add these directories to your compilers include search path (the -I option).
This is because your Makefile contains no tab before giving make command
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
So normally your file should be like below:
obj-m = test.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean

Resources