Adding userspace header files to make file - c

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

Related

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

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

OMAP 3530 cross compile with CodeSourcery to create a simple Linux driver

I have finally setup successfully my OMAP 3530 to boot Embedded Linux using NFS and tftpboot. Now it is time to get into action ie. Write a simple character driver to toggle a LED on the board.
From the CodeSourcery site I have now installed arm-none-linux-gnueabi toolchain. I am having issues building a .ko file.
Usually on x86 machine I will have a makefile that looks like the following:
Makefile:
obj-m += hello-1.o
obj-m += hello-2.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
What changes do I have to do in makefile (and environment variables) so that it builds a .ko file using arm-none-linux-gnueabi toolchain instead of x86 gcc? Also where do I need store and reference Linux kernel header files?
Any examples ( makefile example) or links to resources are appreciated.
You have to export tool chain path and after that you need to export following environment variables
export CROSS_COMPILE=arm-none-linux-gnueabi-
export CC=${CROSS_COMPILE}-gcc
after this run make command to compile files or you can define this environment variables is makefile also.

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

Compiling linux kernel with nvcc

I was trying to write a linux driver which can use the power of GPU via CUDA. The basic Makefile looks like this everywhere:
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
Is there anyway I can put nvcc up there? Basically I want to make my .cu files a part of my linux kernel.
What you are trying to do is impossible.
The CUDA API is a user space API. The basic linux kernel architecture makes it illegal to call any user space API from kernel space code. This include CUDA. IF you want to use the GPU as part of an OS kernel service, you will need to do it via a user space kernel interface like FUSE.

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