Find the answer to your Linux question:
Results 1 to 5 of 5
I have compile the hello program,but failed. in my directory hello, I have include and src directory. In include directory there is hello.h file. In src directory ,there are hello.c ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2006
    Posts
    7

    how write my Makefile for a hello kernel module


    I have compile the hello program,but failed. in my directory hello, I have include and src directory. In include directory there is hello.h file. In src directory ,there are hello.c Makefile fils.
    my hello.h file is as bellow:

    #ifndef HELLO_H
    #define HELLO_H
    #define A 10
    #endif

    My hello.c is as bellow:

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

    #include "hello.h"

    MODULE_LICENSE("GPL");

    static int hello_init(void)
    {
    printk("Hello World!\n");
    printk("a=%d\n", A);
    return 0;
    }

    static void hello_exit(void)
    {
    printk("Good bye!\n");
    }

    module_init(hello_init);
    module_exit(hello_exit);

    My Makfile is as bellow:

    KDIR:=/lib/modules/$(shell uname -r)/build
    PWD :=$(shell pwd)
    obj-m:=hello.o
    default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
    clean:
    $(RM) .*.cmd *.o *.ko -r .tmp*

    when i was in src directory , i run make ,but have some errons;
    [y041138@302wan hello]$ make
    make -C /lib/modules/2.6.8.1/build SUBDIRS=/home/y041138/hello modules
    make[1]: Entering directory `/usr/src/linux-2.6.8.1'
    CC [M] /home/y041138/hello/hello.o
    /home/y041138/hello/hello.c:4:19: hello.h: No such file or directory
    /home/y041138/hello/hello.c: In function `hello_init':
    /home/y041138/hello/hello.c:10: error: `A' undeclared (first use in this functi)
    /home/y041138/hello/hello.c:10: error: (Each undeclared identifier is reported e
    /home/y041138/hello/hello.c:10: error: for each function it appears in.)
    make[2]: *** [/home/y041138/hello/hello.o] Error 1
    make[1]: *** [_module_/home/y041138/hello] Error 2
    make[1]: Leaving directory `/usr/src/linux-2.6.8.1'
    make: *** [default] Error 2
    [y041138@302wan hello]$

    there is hello.h file in include ,why said that there is no hello.h file??
    I guess there is something wrong in my Makefile
    can you help me ,and write a right Makefile or let me get pass the compiler

    but if the hell.h hell.c Makefile are in the same directory, i can pass the compile if i run make in the src directory.

  2. #2
    Just Joined!
    Join Date
    Mar 2006
    Posts
    9
    Code:
    obj-m    := hello.o
    
    KDIR    := /lib/modules/$(shell uname -r)/build
    PWD    := $(shell pwd)
    
    default:
            $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
    umm..mm I suppost that ,you should to insert __int and __exit macro at init module func. and also cleanup....
    ps. in case of you have only one object file

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Posts
    29
    hi, how we define compiler optimazation e.g. -O2 in this makefile?
    thanks

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,757

    Exclamation

    Check this article's Makefile - It worked for me
    http://tldp.org/LDP/lkmpg/2.6/html/
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  5. #5
    Just Joined!
    Join Date
    Dec 2007
    Posts
    29
    it does not help the above link. sorry.
    but I think i found it, check
    http://i.cs.hku.hk/~chyu/linux/modules.html

Posting Permissions

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