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

    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

    My hello.c is as bellow:

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

    #include "hello.h"


    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");


    My Makfile is as bellow:

    KDIR:=/lib/modules/$(shell uname -r)/build
    PWD :=$(shell pwd)
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
    $(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/ SUBDIRS=/home/y041138/hello modules
    make[1]: Entering directory `/usr/src/linux-'
    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-'
    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
    obj-m    := hello.o
    KDIR    := /lib/modules/$(shell uname -r)/build
    PWD    := $(shell pwd)
            $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules 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
    hi, how we define compiler optimazation e.g. -O2 in this makefile?

  4. $spacer_open
  5. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    3rd rock from sun - Often seen near moon


    Check this article's Makefile - It worked for me
    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 Online Linux Terminal

  6. #5
    it does not help the above link. sorry.
    but I think i found it, check

Posting Permissions

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