Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Kernel Module programming...

    I have just had a first attempt at this, using the simple 'Hello World' module :

    #define MODULE
    #include <linux/module.h>
    int init_module(void)
    printk("<1>Hello, world\n");
    return 0;
    void cleanup_module(void)
    printk ("<1>Goodbye cruel world\n");
    Now this is the 'standard' example used in just about all ttutorials and books (some have a few extra bits like #include <linux/hernel.h> and such - but the much the same).

    When i try to compile with the command
    gcc -c hello.c as suggested in one book - I get a huge list of errors (mostly about things not being declared, or defined.. etc.)

    Any pointers?

  2. #2
    Linux User fernape's Avatar
    Join Date
    Dec 2005

    Try this

    You tried the old way to compile modules, now in 2.6 series this is do in this way:

    obj-m += hello-1.o

    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

    Best regards!

  3. #3
    Thanks for your reply.
    I also came across this link which proved useful.. (says the same thing that you said..)

  4. $spacer_open
  5. #4

    try this

    Adding a module to Linux Kernel

    module name:hello.c

    This is a simple module that will be inserted into the kernel.
    when it is inserted into Kernel it display a message Hello world
    When it is removed from the Kernel it display a message Goodbye, world

    #ifndef __MODULE__
    #define __MODULE__

    #ifndef __KERNEL__
    #define __KERNEL__

    #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_2_init(void)
    printk(KERN_EMERG "Hello, world 2\n");
    return 0;

    static void __exit hello_2_exit(void)
    printk(KERN_EMERG "Goodbye, world 2\n");



    Compilation steps:

    1.Create a Makefile ( it will be used bu make command)
    contents in make file
    KDIR:=/lib/modules/$(shell uname -r)/build

    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
    $(RM) *.ko *.mod.c *.mod.o *.o

    Once the makefile is ready, use the following command to compile


    Message on the console after each command is executed

    [root@localhost new]# make
    make -C /lib/modules/2.6.18-1.2798.fc6/build SUBDIRS=/root/Desktop/new modules
    make[1]: Entering directory `/usr/src/kernels/2.6.18-1.2798.fc6-i586'
    CC [M] /root/Desktop/new/hello.o
    Building modules, stage 2.
    CC /root/Desktop/new/hello.mod.o
    LD [M] /root/Desktop/new/hello.ko
    make[1]: Leaving directory `/usr/src/kernels/2.6.18-1.2798.fc6-i586'
    [root@localhost new]#

    2.insmod hello.ko
    [root@localhost new]# insmod hello.ko
    [root@localhost new]#
    Message from syslogd@localhost at Tue Aug 28 12:38:12 2007 ...
    localhost kernel: Hello, world 2


    4.lsmod | less
    Module Size Used by
    hello 5632 0
    autofs4 25413 2
    hidp 24129 2
    rfcomm 45912 0
    l2cap 31681 10 hidp,rfcomm
    bluetooth 58917 5 hidp,rfcomm,l2cap
    sunrpc 158333 1
    ip_conntrack_ftp 12081 0
    ip_conntrack_netbios_ns 7105 0
    ipt_REJECT 9665 1
    xt_state 6337 6
    ip_conntrack 56993 3 ip_conntrack_ftp,ip_conntrack_netbios_ns,xt_stat
    nfnetlink 11353 1 ip_conntrack
    iptable_filter 7233 1
    ip_tables 17669 1 iptable_filter
    ip6t_REJECT 9537 1
    xt_tcpudp 7361 21
    ip6table_filter 7105 1
    ip6_tables 18821 1 ip6table_filter
    x_tables 18501 6 ipt_REJECT,xt_state,ip_tables,ip6t_REJECT,xt_tcp

    [root@localhost new]# rmmod hello
    [root@localhost new]#
    Message from syslogd@localhost at Tue Aug 28 12:43:51 2007 ...
    localhost kernel: Goodbye, world 2

    6.make clean
    [root@localhost new]# make clean
    rm -f *.ko *.mod.c *.mod.o *.o

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts