Find the answer to your Linux question:
Results 1 to 5 of 5
My very first attempt to compile a simple kernel module has gone waste, This is the code for the module: Code: //Our first kernel program!! #include <linux/module.h> #include <linux/kernel.h> int ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! kdev981's Avatar
    Join Date
    Dec 2007
    Location
    Hyderabad,(A.P.) India.
    Posts
    14

    Angry Kernel module not compiling


    My very first attempt to compile a simple kernel module has gone waste,

    This is the code for the module:
    Code:
    //Our first kernel program!!
    #include <linux/module.h>
    #include <linux/kernel.h>
    
    int init_module(void)
    {
     printk("<1> Hello Kernel world!!");
     return 0;
    }
    
    void cleanup_module(void)
    {
     printk(KERN_ALERT "Goodbye Kernel world!");
    }
    and the makefile

    Code:
    TARGET  := hello-1
    WARN    := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
    INCLUDE := -isystem /lib/modules/`uname -r`/build/include
    CFLAGS  := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
    CC      := gcc
    
    ${TARGET}.o: ${TARGET}.c
    
    .PHONY: clean
    
    clean:
            rm -rf ${TARGET}.o
    This is taken from the free ebook Linux Kernel Module programming.

    and here's the output:

    Code:
    [root@localhost hello-1]# make
    gcc -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include   -c -o hello-1.o hello-1.c
    In file included from /lib/modules/2.6.23.1-42.fc8/build/include/linux/prefetch.h:14,
                     from /lib/modules/2.6.23.1-42.fc8/build/include/linux/list.h:8,
                     from /lib/modules/2.6.23.1-42.fc8/build/include/linux/module.h:9,
                     from hello-1.c:2:
    /lib/modules/2.6.23.1-42.fc8/build/include/asm/processor.h:83: error: ‘CONFIG_X86_L1_CACHE_SHIFT’ undeclared here (not in a function)
    /lib/modules/2.6.23.1-42.fc8/build/include/asm/processor.h:83: error: requested alignment is not a constant
    In file included from /lib/modules/2.6.23.1-42.fc8/build/include/linux/module.h:20,
                     from hello-1.c:2:
    /lib/modules/2.6.23.1-42.fc8/build/include/asm/module.h:69:2: error: #error unknown processor family
    make: *** [hello-1.o] Error 1
    I wonder how the processor family is unknown, since the same kernel is running on this machine (2.6.23.1-42.fc8 )

    Please help!

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try this makefile, place one real tab before a make statement (not spaces!):

    Code:
    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

    Regards

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    1

    Try this:

    Delete everything inyour Makefile, and add this:

    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

  4. #4
    Just Joined!
    Join Date
    Jan 2008
    Posts
    3
    Hi
    Even i am working on my first hello world program in modifying the kernel. I have just began to get familiarized with linux, so i dont have a lot of practice with the makefiles.
    This is my kernel program

    #define MODULE
    #include <linux/module.h>

    int init_module(void)
    {
    printk("hello\n");
    return 0;
    }

    void cleanup_module(void)
    {
    printk("bbye\n");
    }

    I have been trying to search for the makefile to use for this.
    If i run the program using the gcc command (as stated in the book - O Reilly)

    #gcc -c hello.c

    usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.text+0x1: In function `_start':
    ../sysdeps/i386/elf/start.S:77: undefined reference to `main'
    /tmp/cc6tY8ap.o(.text+0xf): In function `init_module':
    : undefined reference to `printk'
    /tmp/cc6tY8ap.o(.text+0x2c): In function `cleanup_module':
    : undefined reference to `printk'
    collect2: ld returned 1 exit status

    the linux i am using presently is 2.4.20-8
    I have tried to search about this error and similar pots online, but couldnt find anything. Could someone please help me with this? Or if similar question has been asked before to post the link so that i can get my program working

    thanks in advance

    gagan

  5. #5
    Just Joined!
    Join Date
    Jun 2006
    Posts
    29
    include these headers

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



    and compile with
    gcc -c -O2 -Wall -isystem /lib/modules/`uname -r`/build/include -D__KERNEL__ -DMODULE xxxx.c

    spend some time on understanding that copile command it helps you a lot.

    keep coding ...

Posting Permissions

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