LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   linux/module.h does not exist (https://www.linuxquestions.org/questions/programming-9/linux-module-h-does-not-exist-827020/)

tkmsr 08-18-2010 10:20 AM

linux/module.h does not exist
 
I wrote following program in my home directory.
Code:

#include <linux/kernel.h>
#include <sys/syscall.h>
#include <linux/module.h>
extern void *sys_table[];
asmlinkage int(*main_sys_exit)(int);
{
        printk("Sys_exit called with err_code=%d\n",err_code);
        return main_sys_exit(err_code);
}

int init_module()
{
        main_sys_exit=sys_table[__NR_exit];
        sys_table[__NR_exit]=alt_exit_function;
}
void cleanup_module()
{
        sys_table[__NR_exit]=main_sys_exit;
}
~

When I compiled it as
Code:

gcc -Wall -DMODULE -D__KERNEL__  -DLINUX -c sample2.c
I got following error
Code:

sample2.c:3:26: error: linux/module.h: No such file or directory
sample2.c:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
sample2.c:6: error: expected identifier or ‘(’ before ‘{’ token
sample2.c: In function ‘init_module’:
sample2.c:13: error: ‘main_sys_exit’ undeclared (first use in this function)
sample2.c:13: error: (Each undeclared identifier is reported only once
sample2.c:13: error: for each function it appears in.)
sample2.c:14: error: ‘alt_exit_function’ undeclared (first use in this function)
sample2.c:15: warning: control reaches end of non-void function
sample2.c: In function ‘cleanup_module’:
sample2.c:18: error: ‘main_sys_exit’ undeclared (first use in this function)

What is the above error and why is it coming?
I did all this in my home directory.

knudfl 08-19-2010 01:10 AM

You are using a header, only present in the kernel source code.
( ? May be you have /usr/src/linux/include/linux/module.h ? ).
The kernel headers in /usr/incude/linux etc. : headers used when
your glibc = libc6 was created. (And those headers are meant for
application builds.)


gcc -I<path-to-kernel-source>/include
( I don't know / remember the order of the CFLAGS, -I/<> , sample2.c )
.....

tkmsr 08-19-2010 09:29 AM

Ok I did try what you said but it did not worked.

Sergei Steshenko 08-19-2010 09:31 AM

Quote:

Originally Posted by tkmsr (Post 4071292)
Ok I did try what you said but it did not worked.

First find out whether kernel source is installed and whether the file in question is in the kernel source tree.

tkmsr 08-19-2010 10:38 AM

Tried
Code:

sudo apt-get install linux-headers-generic
got following output
Code:

Reading package lists... Done
Building dependency tree     
Reading state information... Done
linux-headers-generic is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 115 not upgraded.

Check this

page 48 left hand column.

Sergei Steshenko 08-19-2010 12:50 PM

Quote:

Originally Posted by tkmsr (Post 4071371)
Tried
Code:

sudo apt-get install linux-headers-generic
got following output
Code:

Reading package lists... Done
Building dependency tree     
Reading state information... Done
linux-headers-generic is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 115 not upgraded.

Check this

page 48 left hand column.

I don't want to sign in into 'google' just to see a document.
  1. Package managers typically allow to see the list of files present in a file;
  2. Any UNIXish system has 'find' command.

- the above two definitely enable checking file presence.

tkmsr 08-19-2010 06:48 PM

Kernel source is installed.
Which file are you pointing to ?
Code:

find linux-headers-2.6.28-11-generic/ -name module.h
linux-headers-2.6.28-11-generic/include/linux/module.h

result of find / -name module.h
Code:

/usr/src/linux-headers-2.6.28-11-generic/include/linux/module.h
/usr/src/linux-headers-2.6.28-11/arch/alpha/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/x86/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/sh/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/ia64/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/ia64/include/asm/sn/module.h
/usr/src/linux-headers-2.6.28-11/arch/arm/mach-ns9xxx/include/mach/module.h
/usr/src/linux-headers-2.6.28-11/arch/arm/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/mips/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/avr32/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/sparc/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/s390/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/cris/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/h8300/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/m68knommu/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/blackfin/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/powerpc/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/parisc/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/include/asm-m68k/module.h
/usr/src/linux-headers-2.6.28-11/include/asm-m32r/module.h
/usr/src/linux-headers-2.6.28-11/include/asm-mn10300/module.h
/usr/src/linux-headers-2.6.28-11/include/linux/module.h
/usr/src/linux-headers-2.6.28-11/include/asm-frv/module.h
/usr/src/linux-headers-2.6.28-11/include/asm-xtensa/module.h
/usr/include/sepol/policydb/module.h
/usr/include/sepol/module.h

Also
I looked at man page of gcc and searched for
DMODULE D__KERNEL and DLINUX got following

Quote:

Pattern not found (press RETURN)

Sergei Steshenko 08-19-2010 07:12 PM

Quote:

Originally Posted by tkmsr (Post 4071782)
Kernel source is installed.
Which file are you pointing to ?
...

Do you understand how 'gcc' '-I<some_path1>' and how "C"

Code:

#include <some_path2/some_file.h>
statement work ?

Assuming the

Code:

#include <linux/module.h>
piece of code should include a file from Linux kernel tree just one answer exists in your case.

C99 standard is: http://www.open-std.org/JTC1/SC22/wg...docs/n1124.pdf . And the item to look for is "Source file inclusion".

'gcc' documentation is, for example, http://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc.pdf , and the item to look for is "Options Controlling the Preprocessor".

tkmsr 08-19-2010 08:11 PM

From the second link you gave
page 125 of the 2nd pdf says
Quote:

Split the include path. Any directories specified with ‘-I’ options before ‘-I-’
-I-
are searched only for headers requested with #include "file "; they are not
searched for #include <file >.
options after the ‘-I-’, those directories are searched for all ‘#include’ direc-
tives.
If additional directories are specified with ‘-I’

In addition, ‘-I-’ inhibits the use of the directory of the current file direc-
tory as the first search directory for #include "file ". This option has been
deprecated.

so does that mean as some one suggested above I should not use -I or should I use it?

also on page 121
Quote:

-D name
Predefine name as a macro, with definition 1.
what does this mean ?

Sergei Steshenko 08-20-2010 06:47 AM

Quote:

Originally Posted by tkmsr (Post 4071837)
From the second link you gave
page 125 of the 2nd pdf says


so does that mean as some one suggested above I should not use -I or should I use it?

also on page 121

what does this mean ?

You need to use '-I' and to properly set path after it.

Regarding

Quote:

Predefine name as a macro, with definition 1.
- it's a simple English sentence, so which word(s) you do not understand ?

Anyway, first concentrate on the '-I'.

tkmsr 08-20-2010 09:50 AM

Quote:

with definition 1
More over the example I quoted I am my self not clear which header files it is using?
Please stay on this thread I want to dig it till I solve it.

Sergei Steshenko 08-20-2010 10:09 AM

Quote:

Originally Posted by tkmsr (Post 4072455)
More over the example I quoted I am my self not clear which header files it is using?
Please stay on this thread I want to dig it till I solve it.

So, which of the three words:
  1. with
  2. definition
  3. 1

is not clear ?

Regarding

Quote:

I am my self not clear which header files it is using?
- in the source code you have

Code:

#include <linux/module.h>
, so, in terms of C99 standard the "linux/module.h" part is "h-char-sequence", and the latter is defined too in the standard. Also, in paragraph 2 of section 6.10.2 the process of search/inclusion is described.

Sergei Steshenko 08-20-2010 10:21 AM

I also suggest to read section 6.10.3 of the C99 standard - after you're done with the '#include' issue.

runcoderen 08-20-2010 06:53 PM

device driver
 
hi tsmsr:
have you resolved the question yet?
if not, please contact me.

email: runcoderzhcn@gmail.com

I have see the same question and resolved it successfully.

tkmsr 08-20-2010 08:06 PM

I had missed one line of code from the link I gave

following is new program
Code:

#include <linux/kernel.h>
#include <sys/syscall.h>
#include <linux/module.h>
extern void *sys_table[];
asmlinkage int(*main_sys_exit)(int);
asmlinkage int alt_exit_function(int err_code)
{
        printk("Sys_exit called with err_code=%d\n",err_code);
        return main_sys_exit(err_code);
}

int init_module()
{
        main_sys_exit=sys_table[__NR_exit];
        sys_table[__NR_exit]=alt_exit_function;
}
void cleanup_module()
{
        sys_table[__NR_exit]=main_sys_exit;
}


Code:

gcc -Wall -DMODULE -D__KERNEL -DLINUX -c sample2.c
sample2.c:3:26: error: linux/module.h: No such file or directory
sample2.c:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
sample2.c:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
sample2.c: In function ‘init_module’:
sample2.c:14: error: ‘main_sys_exit’ undeclared (first use in this function)
sample2.c:14: error: (Each undeclared identifier is reported only once
sample2.c:14: error: for each function it appears in.)
sample2.c:15: error: ‘alt_exit_function’ undeclared (first use in this function)
sample2.c:16: warning: control reaches end of non-void function
sample2.c: In function ‘cleanup_module’:
sample2.c:19: error: ‘main_sys_exit’ undeclared (first use in this function)


Now see I think it is easy to tell now as what is the error.
Also
as some one pointed out about linux/module.h

I did a find on module.h

Code:

/usr/src/linux-headers-2.6.28-11-generic/include/linux/module.h
/usr/src/linux-headers-2.6.28-11/arch/alpha/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/x86/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/sh/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/ia64/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/ia64/include/asm/sn/module.h
/usr/src/linux-headers-2.6.28-11/arch/arm/mach-ns9xxx/include/mach/module.h
/usr/src/linux-headers-2.6.28-11/arch/arm/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/mips/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/avr32/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/sparc/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/s390/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/cris/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/h8300/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/m68knommu/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/blackfin/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/powerpc/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/arch/parisc/include/asm/module.h
/usr/src/linux-headers-2.6.28-11/include/asm-m68k/module.h
/usr/src/linux-headers-2.6.28-11/include/asm-m32r/module.h
/usr/src/linux-headers-2.6.28-11/include/asm-mn10300/module.h
/usr/src/linux-headers-2.6.28-11/include/linux/module.h
/usr/src/linux-headers-2.6.28-11/include/asm-frv/module.h
/usr/src/linux-headers-2.6.28-11/include/asm-xtensa/module.h
/usr/include/sepol/policydb/module.h
/usr/include/sepol/module.h


Which module.h should I include using -I directive as you specified above that is my problem.

After this I tried having a make file

Code:

obj-m+=sample2.o
all:
        make -C /lib/modules/$(uname -r)/build/M=$(PWD) modules
clean:
        make -C /lib/modules/$(uname -r)/build/M=$(PWD) clean

but the above also gave me an error

Code:

make: *** /lib/modules//build/M=/home/tkmsr/LKP/pandora/temp/sample2: No such file or directory.  Stop.
make: *** [all] Error 2

I checked on command prompt
Code:

v=$(uname -r)
 echo $v
2.6.28-11-generic

What else should I try?


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