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 terminatedcould 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) cleanand 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
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 *.orderTo run the code
$make
$ sudo insmode basic.ko
$ dmesg
u will get the output
$ sudo rmmod basic.ko
$ dmesg 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.
GCC gives the error because it cannot find the header file(s) you include. You should:
- check whether the path is not in the
raw1394.cfolder, 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) - Check your compiler path whether there is a file named
linuxwith 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/*.oNote 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.