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"); } 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. | |
11-26-2011, 05:32 AM | #2 |
Senior Member Registered: Apr 2007 Location: Portland, OR Distribution: Debian, Android, LFS Posts: 1,168 Rep: | 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 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. |
11-26-2011, 05:58 AM | #3 |
Member Registered: Nov 2011 Location: Iran Distribution: Debian, CentOS, LFS, CloudLinux Posts: 377 Original Poster Rep: | 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 |
11-26-2011, 06:23 AM | #4 |
Senior Member Registered: Apr 2007 Location: Portland, OR Distribution: Debian, Android, LFS Posts: 1,168 Rep: | 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. |
11-26-2011, 08:03 AM | #5 |
Member Registered: Nov 2011 Location: Iran Distribution: Debian, CentOS, LFS, CloudLinux Posts: 377 Original Poster Rep: | 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 |
11-26-2011, 09:25 AM | #6 |
Senior Member Registered: Apr 2007 Location: Portland, OR Distribution: Debian, Android, LFS Posts: 1,168 Rep: | You need to insert the Kernel Object (.ko) file, hello.ko. hello.o is intermediary object code. |
1 members found this post helpful. |
11-26-2011, 11:03 AM | #7 |
Member Registered: Nov 2011 Location: Iran Distribution: Debian, CentOS, LFS, CloudLinux Posts: 377 Original Poster Rep: | i'm sorry, i didn't get it.what should i do exactly? |
11-26-2011, 11:58 AM | #8 |
Senior Member Registered: Apr 2007 Location: Portland, OR Distribution: Debian, Android, LFS Posts: 1,168 Rep: | 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 |
11-27-2011, 02:52 AM | #9 |
Member Registered: Nov 2011 Location: Iran Distribution: Debian, CentOS, LFS, CloudLinux Posts: 377 Original Poster Rep: | i did insert the hello.ko but nothing happend!!! Code: root@ubuntu:/home/devilboy/Desktop# insmod ./hello.ko root@ubuntu:/home/devilboy/Desktop# |
11-27-2011, 03:21 AM | #10 |
Senior Member Registered: Apr 2007 Location: Portland, OR Distribution: Debian, Android, LFS Posts: 1,168 Rep: | Run dmesg and you'll see it. |
11-28-2011, 11:22 AM | #11 |
Member Registered: Nov 2011 Location: Iran Distribution: Debian, CentOS, LFS, CloudLinux Posts: 377 Original Poster Rep: | tanX |
11-30-2011, 01:41 PM | #12 |
Member Registered: Nov 2011 Location: Iran Distribution: Debian, CentOS, LFS, CloudLinux Posts: 377 Original Poster Rep: | 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); 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. 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 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' |
12-02-2011, 07:28 AM | #13 |
Senior Member Registered: Apr 2007 Location: Portland, OR Distribution: Debian, Android, LFS Posts: 1,168 Rep: | 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 Code: obj-m += hello.o |
12-04-2011, 11:52 AM | #14 |
Member Registered: Nov 2011 Location: Iran Distribution: Debian, CentOS, LFS, CloudLinux Posts: 377 Original Poster Rep: | thank you |
12-05-2011, 02:47 AM | #15 |
Senior Member Registered: Mar 2008 Location: Cyberspace Distribution: Dynebolic, Ubuntu 10.10 Posts: 1,340 Rep: |
결국적으로 여기서 다뤄야할 것은 insmod 와 rmmod 등에 대한 것인데, 이것이 바로 kernel compile 이후에 등장하는 System Call 과 관련이 되어있다. 어쨋든 Linux kernel 을 구성하고 System Call 을 구성하는 근간이긴 하나 여기서는 설명하기엔 정말 쓸데없는 소리라 일단 넘어가본다.