1

I am new to Ubuntu and I am trying to compile a small program. I have a Makefile and raw1394.c file in a folder raw1394.

when I run a make command it should produce raw1394.ko file but I am getting nothing in when I run make command.

I also tried to compile raw1394.c file with gcc but it is giving me following error.

raw1394.c:1:26: fatal error: linux/module.h: No such file or directory
compilation terminated

could you please guide me how can I solve this problem.

here is my Makefie

obj-m += raw1394.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

and here is my raw1394.c file

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

int init_module(void)
{
printk(KERN_INFO "Loaded dummy raw1394 module\n");
return 0;
}

3 Answers 3

3

source file name is basic.c

#include <linux/init.h>
#include <linux/module.h>
/*MODULE_LICENSE("Dual BSD/GPL");*/
static int hello_init(void)
{
    printk(KERN_ALERT "Hello, world\n");
    return 0;
}
static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);

make file for ubuntu

at first type on ur terminal that uname -r then u will get the version.. that is using on ur system .

obj-m +=basic.o

KDIR =/usr/src/linux-headers-3.13.0-44-generic

all:
 $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
 rm -rf *.o *.ko *.mod.* *.symvers *.order

To run the code

$make
$ sudo insmode basic.ko
$ dmesg
u will get the output
$ sudo rmmod basic.ko
$ dmesg
2

In all likelihood you are missing the Linux headers for your kernel (the .h files you include in the first lines of your programme). These files are different for each kernel version, but are conveniently packaged for you in the repos. Try to install it this way:

sudo apt-get install linux-headers-$(uname -r)

Note that your kernel version is being passed through the uname -r command.

1

GCC gives the error because it cannot find the header file(s) you include. You should:

  1. check whether the path is not in the raw1394.c folder, because then you need to use quotes in stead of <>: so #include "linux/module.h" (this might be the case if you do not want to use your own kernel's header files)
  2. Check your compiler path whether there is a file named linux with the header files in it. You're probably missing the Linux headers.

Secondly, makefiles are used for automating the compilation process (since this often involves multiple instructions and/or different instructions for different targets). But you will still need the correct gcc command(s) in your makefile. So your (basic) makefile should look like this (a lot of extensions and flexibility can be added to it, read more about makefiles if you want to do this):

CC=gcc
CFLAGS=-Wall -c

raw: raw1394.c
TAB $(CC) $(CFLAGS) raw1394/raw1394.c

all:
clean:
TAB rm -rf raw1394/*.o

Note that specific intendations are used in Makefiles. Therefore I put TAB in the code where there should actually be a tab (multiple spaces will NOT work).

Typing make raw in the correct directory on the command line will automatically execute target raw in the Makefile.

3
  • thank you for your reply. I have header file module.h in linux folder, so why it is giving me this error?
    – nabeel
    Nov 15, 2013 at 10:19
  • please guide me, I have been trying to solve this problem but no luck.
    – nabeel
    Nov 15, 2013 at 10:26
  • Thank you for your reply, I read you post again and again and found my answer. Actually problem is with TAB. I give the TAB and now it is working
    – nabeel
    Nov 15, 2013 at 11:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .