728x90




fata error 가 compile 도중 init.h 를 찾지 못하며 등장했다. 

You need to build the module within the Context of the Linux tree. By default, the compiler will look for user-space headers in /usr/include. There ARE some linux headers there (/usr/include/linux), but module.h is not one of them, as it deals with kernel constructs directly.

In short, you need a Makefile. Also, get rid of the #define MODULE. save the following to a file called Makefile and run make:



결론은 Linux tree (여기서 Linux tree 라고 언급하는 것은, Linux Kernel 을 컴파일할때 들어가는 필수적인 요소이기 때문에 Linux tree 라고 언급한다.) 기본적으로 컴파일러는 user-space header 들에 대해서 /usr/include 디렉토리 밑에서 검사하는데 (일부 linux header 들은 (/usr/include/linux) 안에 있다), module.h 는 둘 다 포함되지 않는다. 

(이게 뭔 개소리요?!)


결국적으로 이것은 kernel 구성체에서 직접적으로 다뤄줘야 한다는 얘기다.

짧게는 그냥 Makefile 을 만들어서 조작하면 된다. 


상단의 #define MODULE 을 지우고, Makefile 을 부르고 make를 실행시켜보자.


#define MODULE
#include <linux/module.h>
int init_module(void) { printk("<1>Hello, world\n"); return 0; }
void cleanup_module(void) { printk("<1>Goodbye cruel world\n"); }
when i'm gonna compile it, i get this error:
Code:
root@ubuntu:/home/devilboy/Desktop# gcc -c hello.c 
hello.c:2:26: fatal error: linux/module.h: No such file or directory
compilation terminated.
how can i install module.h library and where should it be placed?
 
Old 11-26-2011, 05:32 AM  #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Hi Devil Boy,

You need to build the module within the Context of the Linux tree. By default, the compiler will look for user-space headers in /usr/include. There ARE some linux headers there (/usr/include/linux), but module.h is not one of them, as it deals with kernel constructs directly.

In short, you need a Makefile. Also, get rid of the #define MODULE. save the following to a file called Makefile and run make:

Code:
obj-m += foo.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
This triggers the kernel build system.

Given that you are using Ubuntu, you probably already have the kernel headers installed at /usr/src/linux-headers-$(uname -r). module.h lives here:

Code:
jameson@aqua:~$ ls /usr/src/linux-headers-$(uname -r)/include/linux/module.h
/usr/src/linux-headers-3.0.0-12-generic/include/linux/module.h

Last edited by jhwilliams; 11-26-2011 at 05:39 AM.
 
Old 11-26-2011, 05:58 AM  #3
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Original Poster 
Rep: Reputation: 8
i did what you said i created a directory named header and i put the Makefile in it, but i got this error:
Code:
root@ubuntu:/home/devilboy/Desktop/Header# make
make -C /lib/modules/3.0.0-12-generic/build M=/home/devilboy/Desktop/Header modules
make[1]: Entering directory `/usr/src/linux-headers-3.0.0-12-generic'
make[2]: *** No rule to make target `/home/devilboy/Desktop/Header/foo.c', needed by `/home/devilboy/Desktop/Header/foo.o'.  Stop.
make[1]: *** [_module_/home/devilboy/Desktop/Header] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.0.0-12-generic'
make: *** [all] Error 2
 
Old 11-26-2011, 06:23 AM  #4
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Ah, your module is called hello.c, not foo.c. You'd need to change that one first line of the makefile, from foo.o to hello.o.
 
Old 11-26-2011, 08:03 AM  #5
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Original Poster 
Rep: Reputation: 8
i did what you said and i could compile it . but i can't insert it as module:
Code:
root@ubuntu:/home/devilboy/Desktop# insmod ./hello.o 
insmod: error inserting './hello.o': -1 Invalid module format
 
Old 11-26-2011, 09:25 AM  #6
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
You need to insert the Kernel Object (.ko) file, hello.ko. hello.o is intermediary object code.
 
1 members found this post helpful.
Old 11-26-2011, 11:03 AM  #7
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Original Poster 
Rep: Reputation: 8
i'm sorry, i didn't get it.what should i do exactly?
 
Old 11-26-2011, 11:58 AM  #8
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
hello.o is not the file you want to insert. You want to insert the kernel module, which is hello.ko.
Code:
insmod hello.ko
 
Old 11-27-2011, 02:52 AM  #9
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Original Poster 
Rep: Reputation: 8
i did insert the hello.ko but nothing happend!!!
Code:
root@ubuntu:/home/devilboy/Desktop# insmod ./hello.ko 
root@ubuntu:/home/devilboy/Desktop#
shouldn't it show me a message(Hello, world)?
 
Old 11-27-2011, 03:21 AM  #10
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
Run dmesg and you'll see it.
 
Old 11-28-2011, 11:22 AM  #11
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Original Poster 
Rep: Reputation: 8
tanX
 
Old 11-30-2011, 01:41 PM  #12
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Original Poster 
Rep: Reputation: 8
i'm having the same problem with init.h
hello.c:
Code:
#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);
gcc -c hello.c:
Code:
root@ubuntu:/home/devilboy/Desktop# gcc -c hello.c 
hello.c:1:24: fatal error: linux/init.h: No such file or directory
compilation terminated.
i created the Makefile like this
Code:
obj-m += hello.c
all:
		make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
		make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
make Makefile:
Code:
root@ubuntu:/home/devilboy/Desktop# make
make -C /lib/modules/3.0.0-12-generic/build M=/home/devilboy/Desktop modules
make[1]: Entering directory `/usr/src/linux-headers-3.0.0-12-generic'
scripts/Makefile.build:310: target `/home/devilboy/Desktop/hello.c' doesn't match the target pattern
  Building modules, stage 2.
  MODPOST 0 modules
make[1]: Leaving directory `/usr/src/linux-headers-3.0.0-12-generic'
 
Old 12-02-2011, 07:28 AM  #13
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
You shouldn't be running gcc directly. That's the problem. Just run make. Also, that won't work at this point because you have an error in your makefile.

Code:
obj-m += hello.c
should read:

Code:
obj-m += hello.o
 
Old 12-04-2011, 11:52 AM  #14
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS, CloudLinux
Posts: 377

Original Poster 
Rep: Reputation: 8
thank you
 
Old 12-05-2011, 02:47 AM  #15
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
Quote:
Originally Posted by jhwilliams View Post
You need to insert the Kernel Object (.ko) file, hello.ko. hello.o is intermediary object code.
I'd love to find out a bit more about this - so there are TWO files generated then? What then, is "hello.o", and why is it generated?


Thnx.



결국적으로 여기서 다뤄야할 것은 insmod 와 rmmod 등에 대한 것인데, 이것이 바로 kernel compile 이후에 등장하는 System Call 과 관련이 되어있다. 어쨋든 Linux kernel 을 구성하고 System Call 을 구성하는 근간이긴 하나 여기서는 설명하기엔 정말 쓸데없는 소리라 일단 넘어가본다.

+ Recent posts