Find the answer to your Linux question:
Results 1 to 9 of 9
These two files i have created in som efolder in home dir 1)hello-1.c 2)Makefile #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_INFO */ int ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2006
    Posts
    22

    need help to start kernel module programming


    These two files i have created in som efolder in home dir
    1)hello-1.c
    2)Makefile


    #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−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



    ------------------------------------------------------------------------------

    when i tried
    $ make
    make: Nothing to be done for `all'.


    i tried to compile the c file directly with gcc.

    it is not able to find kernel.h and module.h

    then i gave the whole path . even then it is giving some library errors only .

    How can i made this first step SUCCESSFULL.,

    pls help me.. i am very much interested in learning kernel.

    thanks & regards
    -vinay

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631

    Thumbs up

    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
    Jul 2006
    Posts
    22
    thanks franklin!

    i changed the make file as u said. now it has started compiling the file.

    still it is not successfully compiling. here is the error after doing "make"
    --------------------------------------------------------------------------------------------------------
    make -C /lib/modules/2.6.23.1-42.fc8 /build M=/root/myComp/research/linKer/hello-1 modules
    make[1]: Entering directory `/lib/modules/2.6.23.1-42.fc8'
    make[1]: *** No rule to make target `/build'. Stop.
    make[1]: Leaving directory `/lib/modules/2.6.23.1-42.fc8'
    make: *** [all] Error 2
    --------------------------------------------------------------------------------------------------------

    "[all] Error 2" where does the actual error happened...!

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined! amit4g's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore,India
    Posts
    63
    Do you have the kernel sources (`uname -r`) under /lib/modules

    i.e.
    [amit@localhost modules]$ pwd
    /lib/modules
    [amit@localhost modules]$ ls `uname -r`/source
    arch CREDITS Documentation include Kbuild MAINTAINERS Module.symvers REPORTING-BUGS sound usr
    block crypto drivers init kernel Makefile net scripts System.map vmlinux
    COPYING cscope.out fs ipc lib mm README security tmp vmlinux.o


    ~amit

  6. #5
    Just Joined!
    Join Date
    Jun 2006
    Posts
    29
    I suggest u to use below commands to compile ur module
    Code:
    #gcc -c -O2 -Wall -isystem /lib/modules/`uname -r`/build/include -D__KERNEL__ -DMODULE xxxxx.c
    if ur trying #make it wont work , it works with #make modules and still it need to support loadable kernel module.

    Anyhow go through Makefile programming once to understand

  7. #6
    Just Joined!
    Join Date
    Jul 2006
    Posts
    22
    Ashok! and amit thanQ.!
    i wil go thru MAKE FILE programming .!

    ashok ,.! i tried with ur string.! it gave me following error.

    one thing is : "#make modules and still it need to support loadable kernel module."

    i have heard this before .. currently i am using FC8 and KUBUNTU 7.04 linux= for practicing this stuff, How can me make a kernel.. to module loadable kerel???????

    do we really need makefile programming?? before starting kernel programming??

    ================================================== ================
    [root@localhost hello-1]# gcc -c -O2 -Wall -isystem /lib/modules/`uname -r`/build/include -D__KERNEL__ -DMODULE 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:1:
    /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:1:
    /lib/modules/2.6.23.1-42.fc8/build/include/asm/module.h:69:2: error: #error unknown processor family

  8. #7
    Just Joined!
    Join Date
    Jun 2006
    Posts
    29
    Makefile prograaming is simple all you need to do is understanding normal makefile and device driver makefile.


    Re-compile kernel to support loadable modules

    #make menuconfig --> check loadable kernel module support
    #make
    #make modules
    #make modules_install

    try
    #make help --for more

    Go through any document on Kernel compilation before you start, just google..

  9. #8
    Just Joined!
    Join Date
    Jul 2006
    Posts
    22
    thanks Ashok..

    on emistake i have done in make file.

    i included a space b4 /build.
    "make −C /lib/modules/$(shell uname −r)/build M=$(PWD) modules"

    all .o .ko files are created..


    thanks alot .!

  10. #9
    Just Joined!
    Join Date
    Jul 2006
    Posts
    22
    ashok1

    jus now i have done insmod hello-1.ko

    now os is behaving strangely
    keys are not functioning well. greater than symbol is not working .. same with grep symbol.

    also.. when i tried to see in /proc/modules file. hello-1.ko is no where

    even in modprobe it is not listed...
    why is kernel behaving like this... did it gone panic.. .

Posting Permissions

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