fedora linux --- compiling first linux module - linux

Getting error while compiling my first kernel module in Fedora linux.
Source code :--
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}
static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye Mr.\n");
}
module_init(hello_start);
module_exit(hello_end);
Makefile :----
obj-m = hello.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
error at make time :--
$ make
make -C /lib/modules/3.8.6-203.fc18.x86_64/build M=/home/dinesh/development/linux/kernel_modules/hello modules
make: *** /lib/modules/3.8.6-203.fc18.x86_64/build: No such file or directory. Stop.
make: *** [default] Error 2
Now if i see build is there or not , i get following o/p. Build is shown as an softlink :---
$ ls -l /lib/modules/3.8.6-203.fc18.x86_64/
total 2632
lrwxrwxrwx. 1 root root 38 Apr 15 21:32 build -> /usr/src/kernels/3.8.6-203.fc18.x86_64
drwxr-xr-x.
I got same error even after installing, kernel-devel :--
My makefile is correct it have correct tab before rule. Please suggest how to resolve this error ?

As guido said, you have to match your current kernel and the kernel-devel package.
To get your kernel version run
uname -r
I get 3.6.10-4.fc18.x86_64, download that kernel-devel version
sudo yum install kernel-devel-3.6.10-4.fc18
Or update your system and boot with the new kernel, I believe those two will automatically match.

Related

Trying to compile hello world linux kernel module: missing MODULE_LICENSE()

I'm trying to compile the simplest kernel module possible, from the The Linux Kernel Module Programming Guide. hello.c:
#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
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
Output of make:
make -C /lib/modules/5.14.0/build M=/home/parallels/dev/demo modules
make[1]: Entering directory '/home/parallels/linux-5.14'
CC [M] /home/parallels/dev/demo/hello.o
MODPOST /home/parallels/dev/demo/Module.symvers
ERROR: modpost: missing MODULE_LICENSE() in /home/parallels/dev/demo/hello.o
make[2]: *** [scripts/Makefile.modpost:150: /home/parallels/dev/demo/Module.symvers] Error 1
make[2]: *** Deleting file '/home/parallels/dev/demo/Module.symvers'
make[1]: *** [Makefile:1766: modules] Error 2
make[1]: Leaving directory '/home/parallels/linux-5.14'
make: *** [Makefile:4: all] Error 2
This is all after I have compiled the linux kernel from source and run make modules_install. I don't really understand that second line of output: make[1]: Entering directory '/home/parallels/linux-5.14', that is the directory in which I downloaded and compiled the linux kernel, I don't see what it has to do with this module I'm trying to compile.
I have seen others ask about this missing MODULE_LICENSE() error, but they are usually asking about much more complicated modules and the answers don't seem relevant to this very simple module. A few of the answers had to do with changing the name of either the source code file or the .o file, but trying either way simply produced errors about no rule to make target with the changed name.
Also, this is all on a vm of Kali linux running linux kernel 5.14.0 which I compiled from source.
You need to add call to MODULE_LICENSE to your source file (hello.c in your case). This is what the error message is talking about. E.g.
MODULE_LICENSE("GPL");
Most modules also call MODULE_AUTHOR:
MODULE_AUTHOR("your-name");

unrecognized command line option ‘-fstack-protector-strong’ with arm-linux-gnueabihf-gcc

I have cloned the Raspberry Pi toolchain using the following command and added the tools path to bashrc
git clone https://github.com/raspberrypi/tools.git --depth=1
echo PATH=$PATH:~raspberry/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin >> ~/.bashrc
source ~/.bashrc
Cloned the raspberry pi kernel sources
git clone --depth=1 https://github.com/raspberrypi/linux
Simple hello world module
#include <linux/kernel.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int test_hello_init(void)
{
printk(KERN_INFO"%s: In init\n", __func__);
return 0;
}
static void test_hello_exit(void)
{
printk(KERN_INFO"%s: In exit\n", __func__);
}
module_init(test_hello_init);
module_exit(test_hello_exit);
Makefile
obj-m := hello.o
all:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C /home/jamal/raspberrypi/linux M=$(PWD) modules
clean:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C /home/jamal/raspberrypi/linux M=$(PWD) clean
It's failing with the following error:
arm-linux-gnueabihf-gcc: error: unrecognized command line option ‘-fstack-protector-strong’
scripts/Makefile.build:309: recipe for target '/home/jamal/Linux_Device_Drivers/hello.o' failed
Previously, this used to work, not sure what has changed and now i am getting this error

Unable to insmod hello_world kernel module in debian 8

I can't get why insmod gives Invalid parameters error (can't see anything in dmesg):
$ sudo insmod hello.ko
insmod: ERROR: could not insert module hello.ko: Invalid parameters
$ sudo insmod /hello.ko
insmod: ERROR: could not load module /hello.ko: No such file or directory
I have no parameters in my module. It is just hello world example.
My environment:
$ uname -r
3.16.0-4-amd64
I have installed all possible kernel headers packages:
linux-headers-3.16.0-4-all
linux-headers-3.16.0-4-all-amd64
linux-headers-3.16.0-4-amd64
linux-headers-3.16.0-4-common
linux-headers-amd64
My code:
#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");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
MODULE_LICENSE("GPL");
I use following Makefile:
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
make output:
$ make
make -C /lib/modules/3.16.0-4-amd64/build M=/home/user/c.driver/driver-1 modules
make[1]: Entering directory '/usr/src/linux-headers-3.16.0-4-amd64'
Makefile:10: *** mixed implicit and normal rules: deprecated syntax
make[1]: Entering directory `/usr/src/linux-headers-3.16.0-4-amd64'
CC [M] /home/user/c.driver/driver-1/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/user/c.driver/driver-1/hello.mod.o
LD [M] /home/user/c.driver/driver-1/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-3.16.0-4-amd64'
Update: same result with 14.04.1-Ubuntu
maybe it's because you forget that:
module_init(init_module);
module_exit(cleanup_module);
and I usually declare init_module() and cleanup_module() as a static function.
and fellowing code are my kernel module template:
#include <linux/module.h>
#include <linux/kernel.h>
static int init_module(void)
{
...
return 0;
}
static void exit_module(void)
{
...
}
module_init(init_module);
module_exit(exit_module);
MODULE_LICENSE("GPL");
For me, the issue was that that module file was in a shared folder (in fact, my Ubuntu box is a VM on Parallels). Copy the module to a local folder and try again.
Thanks to #avasin for this. The answer was in the comments but not quick to find, so adding it here as it may help others. This held me up for a couple of hours.

How to compile a kernel module for Raspberry pi?

I'm having trouble compiling a kernel module for a raspberry pi. I want to compile a "hello world" kernel module using the raspberry pi itself.
I am using raspbian wheezy 3.6.11+.
I tried following the directions at http://elinux.org/RPi_Kernel_Compilation.
Here is the Makefile I am using:
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
Here is the source code for hello-1.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");
}
Here's what I get when I try to make the project:
root#raspberrypi:/home/pi/hello-module# make
make -C /lib/modules/3.6.11+/build M=/home/pi/hello-module modules
make: *** /lib/modules/3.6.11+/build: No such file or directory. Stop.
make: *** [all] Error 2
I tried creating the build directory at /lib/modules/3.6.11+
make -C /lib/modules/3.6.11+/build M=/home/pi/hello-module modules
make[1]: Entering directory `/lib/modules/3.6.11+/build'
make[1]: *** No rule to make target `modules'. Stop.
make[1]: Leaving directory `/lib/modules/3.6.11+/build'
make: *** [all] Error 2
I have GNU Make 3.81 and gcc (Debian 4.6.3-14+rpi1) 4.6.3 installed. I also installed the linux source using
sudo apt-get install linux-source
Any ideas on what I might do to get this to compile?
Here are the steps I used to build the Hello World kernel module on Raspbian.
Perform sudo rpi-update
See https://github.com/Hexxeh/rpi-update for details on
rpi-update. You have to be on the latest firmware and associated kernel to be able to perform the next step.
Install and run rpi-source to install the source code that built the latest kernel that you are running. This will create the correct entry in /lib/modules for the kernel that you are running. Note: you don't need to be root to run this, however the script will perform certain tasks using sudo and the root password will be requested during the script execution.
Instructions to install rpi-source can be found at https://github.com/notro/rpi-source/wiki
Once those steps are performed you should be able to make the Hello World kernel module.
johnma#raspberrypi ~/HelloWorld $ make
make -C /lib/modules/3.12.19+/build M=/home/johnma/HelloWorld modules
make[1]: Entering directory `/home/johnma/linux-c3db7205bcd8988cf7c185e50c8849542554b1f5'
CC [M] /home/johnma/HelloWorld/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/johnma/HelloWorld/hello.mod.o
LD [M] /home/johnma/HelloWorld/hello.ko
make[1]: Leaving directory `/home/johnma/linux-c3db7205bcd8988cf7c185e50c8849542554b1f5'
johnma#raspberrypi ~/HelloWorld $ sudo insmod hello.ko
johnma#raspberrypi ~/HelloWorld $ tail -1 /var/log/syslog
May 15 13:45:39 raspberrypi kernel: [59789.169461] Hello World :)
johnma#raspberrypi ~/HelloWorld $ sudo rmmod hello.ko
johnma#raspberrypi ~/HelloWorld $ tail -1 /var/log/syslog
May 15 13:46:10 raspberrypi kernel: [59819.503503] Goodbye World!
When compiling a module the -C parameter should point to the source tree where the kernel was built (don't clean it up!). If you built it on the pi its likely in a directory under your home directory.
The build directory under /lib/modules/<version> is a Debian-ism, where a cut-down version of the source tree is provided with just enough context to build modules against. The kernels from the Raspberry Pi Foundation kernels don't ship with a build directory.
They may be a bit out of date, but raspbian provides a kernel as a Debian-style package, which should include the build directory you could use to build kernel modules against.
sudo aptitude install linux-image-rpi-rpfv linux-headers-rpi-rpfv
You first need kernel headers (and the corresponding kernel binary) to build your module.
Like Greg said, the raspbian distribution provides the packages :
sudo apt-get install linux-image-rpi-rpfv linux-headers-rpi-rpfv
Then, tell raspbian to boot your newly installed kernel (3.10-3-rpi for me).
Append this at end of /boot/config.txt and reboot your Pi :
# Parameters to boot on raspbian kernel (linux-image-rpi-rpfv package)
kernel=vmlinuz-3.10-3-rpi
initramfs initrd.img-3.10-3-rpi followkernel
Then, modify your Makefile to point the freshly installed kernel headers :
KERNEL_HEADERS=/lib/modules/$(shell uname -r)/build
obj-m := hello-1.o
all:
#$(MAKE) -C $(KERNEL_HEADERS) M=$(PWD) modules
clean:
#$(MAKE) -C $(KERNEL_HEADERS) M=$(PWD) clean
This was a pain. I had to compile and install a kernel mode driver.After long search, i got the headers for pi 2 (3.18.7-v7+) from here.
sudo apt-get install dkms build-essential
wget http://www.niksula.hut.fi/~mhiienka/Rpi/linux-headers-rpi/linux-headers-3.18.7-v7%2b_3.18.7-v7%2b-2_armhf.deb
sudo dpkg -i linux-headers-3.18.7-v7+_3.18.7-v7+-2_armhf.deb
I meet with the same problem and just fix it by sudo apt-get install raspberrypi-kernel-headers
I was working on the exact same sample on my RPI with the exact same kernel. I managed to compile the module on my RPI but when I issued insmod I received an error. I followed the instructions here on an XUbuntu virtual machine (using my RPI's kernel version 3.6.y) and it worked perfectly. Not sure why compiling directly on the RPI did not work, that will be a problem for another day.
I had to change the Makefile to match the new environment.
obj-m += hello-1.o
all:
make ARCH=arm CROSS_COMPILE=${CCPREFIX} -C /home/cstick/rpi/linux-rpi-3.6.y M=$(PWD) modules
clean:
make -C /home/cstick/rpi/linux-rpi-3.6.y M=$(PWD) clean

Building HelloWorld C++ Program in Linux with ncurses

I successfully ran sudo apt-get install libncurses5-dev
Within my Eclipse window I then try to build the following HelloWord.cpp program:
#include <ncurses.h>
int main()
{
initscr(); /* Start curses mode */
printw("Hello World !!!"); /* Print Hello World */
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return 0;
}
I get the following error:
Invoking: GCC C++ Linker
g++ -m32 -lncurses -L/opt/lib -o "Test_V" ./src/curseTest.o ./src/trajectory.o ./src/xJus-epos.o -lEposCmd
/usr/bin/ld: cannot find -lncurses
collect2: error: ld returned 1 exit status
make: *** [Test_V] Error 1
It looks like the compiler is searching for the ncurses library and can't find it? I checked /usr/lib and the library does not exist there so do I need to manually link the ncurses library there - I thought the get-apt installer would automatically do this?
g++ HelloWorld.cpp -lncurses -o HelloWolrd
If you have a 32-bit machine, gcc compile m32 auto. If you have a 64-bit machine and you want to compile 32bits you
Your arguments are not in the correct order. You must specify all source files first and then linker search directories before specifying the libraries to link with. Your command should be like this:
g++ HelloWorld.o -L/opt/lib -lncurses -o HelloWorld
Taken from comment by #ChrisDodd:
Your options are in the wrong order -- -L must be BEFORE -l and both must be after all .o

Resources