LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2012, 03:28 PM   #1
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
problem with ubuntu in modules programming !


linux/module.h problem
im just trying to compile this code

Quote:
#define MODULE
#include <linux/module.h>

int init_module (void) /* Loads a module in the kernel */
{
printk("Hello kernel n");
return 0;
}

void cleanup_module(void) /* Removes module from kernel */
{
printk("GoodBye Kerneln");
}
the compiler is showing this error

Quote:
gizmo@ubuntu:~/driver$ sudo gcc -c hello.c
hello.c:2:26: error: linux/module.h: No such file or directory


can anyone help me please?


and i find this answer

Replace the KDIR with the path where your linux sources are.

& i am using ubuntu 12.04 !
with 3.2.0-23-generic-pae

Last edited by tushar_pandey; 07-29-2012 at 03:47 PM.
 
Old 07-29-2012, 04:00 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
(Do you really think kernel programming is for beginners?)
You should have a file called /usr/include/linux/module.h -- if not, you have to install package kernel-source or kernel-headers.

PS: and please use those famous [code] and [/code] tags; you have been asked countless times to do so.

Last edited by NevemTeve; 07-29-2012 at 04:01 PM.
 
Old 07-29-2012, 04:10 PM   #3
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
@nevem teve

Sir , please help me , i want to create my own product in c langauge but i know that i have not enough knowledge for creating that product , so i am trying this .

but i am beginner !

please give me solution , i love c language , but its too typical for me , to join the programming of device drivers , i want to code at higher level , like where i can create something good !

&you know , whenever i see your comment , i think that today i will get something new from you !

Last edited by tushar_pandey; 07-29-2012 at 04:11 PM.
 
Old 07-29-2012, 04:34 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by tushar_pandey View Post
can anyone help me please?
You don't use GCC (gcc) directly when compiling a Kernel module; use a Makefile instead.

Here's an example Makefile that could be used for a file named 'hello.c':

Code:
obj-m := hello.o        # Module Name is hello.c

KDIR  := /lib/modules/$(shell uname -r)/build

all:
        $(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
        $(MAKE) -C $(KDIR) M=$(PWD) clean
You do NOT need to be root (via sudo) to compile your Kernel module into a kernel object file. You will however need to be root to install (insmod) and remove (rmmod) it from the running kernel.

If any of what I have stated is above your current comprehension, then I suggest that you pursue learning the basics of C programming, then move on perhaps to learning about Makefiles. A good online tutorial for C programming is available for download at: http://beej.us/guide/bgc/
 
Old 07-30-2012, 04:58 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You should ask help from a competent linux-admin to do the following things:

1. install kernel source under /usr/src/linux
(most likely via a symlink to the actual location, eg:

/usr/src/linux -> /usr/local/src/linux
/usr/local/src/linux -> /usr/local/src/linux-3.3.7

2. Create symlinks in /usr/include (after renaming the previous content):

Code:
cd /usr/include
ln -s ../src/linux/include/linux .
ln -s ../src/linux/include/scsi .
ln -s ../src/linux/include/asm-generic .
ln -s ../src/linux/arch/x86/include/asm .
Check:
Code:
ls -ld linux scsi asm asm-generic
asm         -> ../src/linux/arch/x86/include/asm
asm-generic -> ../src/linux/include/asm-generic
linux       -> ../src/linux/include/linux
scsi        -> ../src/linux/include/scsi
When it is done, #include <linux/module.h> will work. But, as I've already warned you, it's not for beginners.

Last edited by NevemTeve; 07-30-2012 at 08:19 AM.
 
Old 07-30-2012, 05:44 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by NevemTeve View Post
You should ask help from a competent linux-admin to do the following things:

1. install kernel source under /usr/src/linux
Not every Linux distro uses the location /usr/src/linux; perhaps Debian does, but Ubuntu and Redhat/Fedora do not.

However, it is my understanding that the "universal" way to locate the Linux header files is to reference /lib/modules/`uname -r`/build. This target is a symbolic link to the location where the Kernel Header files are located; for example, on my Fedora 16 system, this is at /usr/src/kernels/3.4.2.-1.fc16.x86_64. Under Kubuntu 12.04, it is located at /usr/src/linux-headers-3.2.0-24-generic.

If the OP is unable to locate "linux/module.h", then they need to install the Kernel Header files. How that is accomplished varies from one distro to another. The OP should first attempt to use his system's package manager.
 
Old 07-30-2012, 06:42 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Another method for people who usually compile kernel for themselves:

put the current version of kernel-source into /usr/local/src/linux-x.y.z
then at boot time from rc.local:

Code:
VER=$(uname -r)
ln -sfn "linux-${VER}" /usr/local/src/linux
This way /usr/src/linux is always points to the current source; links linux, scsi, asm and asm-generic in /usr/include point to the right place, too.

PS: this line can also be useful:
Code:
ln -sf "System.map-${VER}" /boot/System.map

Last edited by NevemTeve; 07-30-2012 at 08:18 AM.
 
Old 07-31-2012, 02:27 AM   #8
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
@Tushar_pandey:
Check the below link, it will answer most of your questions.
http://www.freesoftwaremagazine.com/.../drivers_linux
 
Old 07-31-2012, 06:32 AM   #9
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by kauuttt View Post
@Tushar_pandey:
Check the below link, it will answer most of your questions.
http://www.freesoftwaremagazine.com/.../drivers_linux
That does indeed seems like a great tutorial. But the section containing information on how to build a module is incorrect (and has a syntax error). Aside from the hard-coded Kernel version number (which the author acknowledged may not be the same for other systems), not every system stores kernel header files in /usr/src. The usage of "pwd" is also incorrect. In summary, the command will not work on all systems.

The proper way is to use something like:
Code:
make -C /lib/modules/`uname -r`/build M=`pwd` modules
If one hates to type lengthy commands such as the one above, then the solution is to augment the Makefile to contain a similar statement. The OP can refer to my previous post for an example.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pluggable Kernel Modules Programming Last Attacker Programming 0 05-02-2006 03:31 PM
Re: modprobe: Note: /etc/modules.conf is more recent than lib/modules/2.4.9/modules.d Andy.M Linux - General 1 01-24-2002 01:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration