linux kernel module programming

This is a discussion on linux kernel module programming within the Linux Programming forums, part of the Platform Specific Boards category; i have some trouble compiling a module.. i'm using kernel 2.6 and am trying to compile the following file named ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    10

    linux kernel module programming

    i have some trouble compiling a module.. i'm using kernel 2.6 and am trying to compile the following file named hello.c:
    ----------------------------------------------------------------
    Code:
    #include <linux/module.h>
    #include <linux/kernel.h>
    
    int init_module(void)
    {
            printk("<1>Hello world 1.\n");
            return 0;
    }
    
    void cleanup_module(void)
    {
            printk("Goodbye world 1.\n");
    }
    ----------------------------------------------------------------

    to compile it i'm doing like they say in the kernel source code documentation, i have the following Makefile:
    ----------------------------------------------------------------
    obj-m := hello.o
    ----------------------------------------------------------------
    and use this comando to compile it:
    make -C /usr/src/linux-`uname -r` SUBDIRS=$PWD modules

    it gives tons of errors, but the first its comlaining about not beeing able to find the module.h file included...

    i tryied to include them specifying a full path but nothing worked...
    anyone has a solution?? i would be very very very thankfull
    Last edited by circuit; 11-29-2004 at 06:36 PM.

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Might want to look here. You are probably using a guide for the 2.4 kernel. This should settle some stuff, also here. you will find more articles on the differences. I don't know how you are learning device drivers, but a good source is here.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    10

    still not working...

    i did just like in the web pages you said, now it compiles but insmod complains:

    insmod hello.o
    insmod: error inserting 'hello.o': -1 Invalid module format

    i seen this error before :) but never solved it

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    How are you compiling, do you have the latest version of module-init-tools? What is now your code.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    10

    this is how things are....

    i have a fedora core 2 full installed in my pc, so i supose i have the latest tools....

    my current code is (hello.c):
    Code:
    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/kernel.h>
                                                                                                                                   
    MODULE_LICENSE("Dual BSD/GPL");
                                                                                                                                   
    static int hello_init(void)
    {
    printk("Hello World!\n");
    return 0;
    }
                                                                                                                                   
    static void hello_exit(void)
    {
    printk("Good bye!\n");
    }
                                                                                                                                   
    module_init(hello_init);
    module_exit(hello_exit);

    my make file is (Makefile) :
    Code:
    #
    # Makefile for hello.c file
    #
    KDIR:=/lib/modules/$(shell uname -r)/build
                                                                                                                                   
    obj-m:=hello.o
                                                                                                                                   
    default:
            $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
    clean:
            $(RM) .*.cmd *.mod.c *.o *.ko -r .tmp*
    i just do a make to build the all thing

    it makes a hello.o

    i do a insmod ./hello.o
    and the reulst is:
    insmod: error inserting './hello.o': -1 Invalid module format

  6. #6
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    This is what I get:

    Code:
    root, test/ # lsmod
    Module                  Size  Used by
    nvidia               3465148  12
    
    root, test/ # cat hello.c
    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/kernel.h>
    
    MODULE_LICENSE("Dual BSD/GPL");
    
    static int hello_init(void)
    {
    printk("Hello World!\n");
    return 0;
    }
    
    static void hello_exit(void)
    {
    printk("Good bye!\n");
    }
    
    module_init(hello_init);
    module_exit(hello_exit);
    
    root, test/ # cat Makefile
    #
    # Makefile for hello.c file
    #
    KDIR:=/lib/modules/$(shell uname -r)/build
    
    obj-m:=hello.o
    
    default:
            $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
    clean:
            $(RM) .*.cmd *.mod.c *.o *.ko -r .tmp*
    
    root, test/ # ll
    insgesamt 8,0K
    -rw-r--r--  1 root 286 30. Nov 21:02 hello.c
    -rw-r--r--  1 root 191 30. Nov 21:03 Makefile
    
    root, test/ # make
    make -C /lib/modules/2.6.9-gentoo-r4/build SUBDIRS=/root/test modules
    make[1]: Entering directory `/usr/src/linux-2.6.9-gentoo-r4'
      CC [M]  /root/test/hello.o
      Building modules, stage 2.
      MODPOST
      CC      /root/test/hello.mod.o
      LD [M]  /root/test/hello.ko
    make[1]: Leaving directory `/usr/src/linux-2.6.9-gentoo-r4'
    
    root, test/ # ll
    insgesamt 24K
    -rw-r--r--  1 root  286 30. Nov 21:02 hello.c
    -rw-r--r--  1 root 2,2K 30. Nov 21:05 hello.ko
    -rw-r--r--  1 root  632 30. Nov 21:05 hello.mod.c
    -rw-r--r--  1 root 1,7K 30. Nov 21:05 hello.mod.o
    -rw-r--r--  1 root 1,2K 30. Nov 21:05 hello.o
    -rw-r--r--  1 root  191 30. Nov 21:03 Makefile
    
    root, test/ # insmod hello.ko
    
    root, test/ # lsmod
    Module                  Size  Used by
    hello                   1152  0
    nvidia               3465148  12
    
    root, test/ #
    So I'd say it works. Are you sure you're building against the right kernel? You should be getting a .ko file I suppose.
    Last edited by Nyda; 11-30-2004 at 02:12 PM.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    10
    it still cant get it to work , insmod complians about the same....

    a uname -a returns:
    Linux heaven.eden 2.6.5-1.358 #1 Sat May 8 09:04:50 EDT 2004 i686 athlon i386 GNU/Linux

    2.6 right? can't seem to see the problem here....

  8. #8
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Maybe you need to find a package for kernel headers? Those are installed on Gentoo by default, because they are needed all the time, but a binary distribution probably doesn't need those. They are not required to compile the kernel itself afaik.
    And be sure to use linux-2.6 headers.

    Unfortunatly I don't know much about the kernel build system, so I'm just guessing.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    10
    but the problem isent building it its inserting it, maybe i am forgetting something during the bui.ld process that tells that this module is for this kernel... i don't know...

  10. #10
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by circuit
    but the problem isent building it its inserting it, maybe i am forgetting something during the bui.ld process that tells that this module is for this kernel... i don't know...
    Definetly. As said above the module must be a .ko file. You are trying to insert a normal gcc object. (Which was the way it was before 2.6 I believe).
    As for kernel headers, your module would probably build if you were using the (wrong) 2.4 headers. It would just not work.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    10

    solved...

    don't really know what solved my problem but it works now
    thanks for everything, i have know other problem i'll leave here just for the record, cause i don't think this is the place for the new prob, i am trying to read data from /dev/kmem but it insists
    co give an "Operation not permitted", and i run it has root.

    its a fedora core 2 install, and it has no "se linux"...

    thanks for everything

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linux kernel module programming
    By open_engg in forum Linux Programming
    Replies: 2
    Last Post: 08-05-2007, 05:41 PM
  2. OpenGL on Linux - Kernel Panic
    By psychopath in forum Game Programming
    Replies: 2
    Last Post: 04-15-2006, 05:07 PM
  3. Compiling Linux kernel on Windows
    By jmd15 in forum Linux Programming
    Replies: 9
    Last Post: 04-10-2006, 08:28 AM
  4. Linux kernel not compiling
    By frenchfry164 in forum Tech Board
    Replies: 2
    Last Post: 04-29-2003, 05:10 PM
  5. Function basics
    By sjleonard in forum C++ Programming
    Replies: 15
    Last Post: 11-21-2001, 12:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21