Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Linux kernel - hello world

Tags:

linux

kernel

I am trying to compile the Linux kernel: http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html

I have a simple hello world program hello-1.cpp

#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)
{
    return 0;
}

void cleanup_module(void)
{
}

But I am trying to build it using the Makefile:

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 get a couple of errors.

make -C /home/pacman/linux-2.6.34.11/2.6.35.6-45.fc14.i686/build M=/home/pacman/p1 modules
make: *** /home/pacman/linux-2.6.34.11/2.6.35.6-45.fc14.i686/build: No such file or directory.  Stop.

make: * [all] Error 2

Am I forgetting to define something?

like image 473
user289925 Avatar asked Apr 20 '12 21:04

user289925


People also ask

How do I run a Linux kernel program?

The basic way is to add the code to the kernel source tree and recompile the kernel. A more efficient way is to do this is by adding code to the kernel while it is running. This process is called loading the module, where module refers to the code that we want to add to the kernel.

What is Kern_alert?

An emergency condition; the system is probably dead. 1. KERN_ALERT. A problem that requires immediate attention.


1 Answers

Rename hello-1.cpp to hello-1.c (modules must be written in C) and add the lines:

module_init(init_module);
module_exit(cleanup_module);
like image 64
Fabel Avatar answered Oct 15 '22 23:10

Fabel