ºìÁªLinuxÃÅ»§
Linux°ïÖú

linuxÄں˱à³Ì³õ̽£¡

·¢²¼Ê±¼ä:2011-09-18 15:54:53À´Ô´:ºìÁª×÷Õß:ÎÒÀ´³ÙÁË
´ËÎÄÕÂÀ´×ÔÎÒµÄһƪ²©¿Í£¡
http://blog.chinaunix.net/space.php?uid=26281173&do=blog&id=2856305
»¶Ó­´ó¼Ò·ÃÎÊ£¡

ÕâЩÌìÔÚѧϰlinuxÄں˱à³Ì£¬¾ÍÔÚÕâÀïССµÄshowÒÔÏ¡£
Ê×ÏȱàдÈçϵÄlinux´úÂë¡£²¢ÃüÃûΪhello.c
ÕâÀïÄãÓ¦¸Ã×¢ÒâÁÁµã£º
µÚÒ»¡¢linuxÄں˱à³Ì£¬²»Í¬ÓÚÆÕͨµÄÓû§Ì¬Ïµıà³Ì£»ÓÐÒ»¸öÈë¿ÚµÄmainº¯Êý£»ÕâÀïµÄ¡°main¡±º¯ÊýÊÇmodule_init();ͬʱ»¹ÓÐÒ»¸öÉƺó´¦ÀíµÄº¯Êý£ºmodule_exit()¡£
µÚ¶þ¡¢linuxÄں˱à³ÌÔÚ±àÒëµÄʱºò£¬²»Í¬ÓÚÓû§Ì¬Ïµıà³Ì£»¿ÉÒÔÖ±½ÓʹÓÃgcc±àÒëÆ÷±àÒëÁ´½Ó£¬¾ÍÄܹ»³ÉΪ¿ÉÖ´Ðеģ»¶øÊÇÐèÒª±àдһ¸öMakefileÎļþ£¬²»ÊÇmakefile£¡£¡ÕâÀïÇ£³¶µ½ºÜ¶àµÄÄÚºË̬ϵĶ«Î÷£¬¾ßÌåµÄÎÒҲûŪÇå³þ£¡ÏêϸµÄ»áÔÚºóÃæºÍ´ó¼Ò·ÖÏí¡£

hello.c Îļþ
1 #include
2 #include
3 #include
4 static int __init lkp_init(void);
5 static int __init lkp_exit(void);
6
7
8 static int __init lkp_init(void)
9 {
10 printk("<1>Hello ,Word!\n");
11 return 0;
12 }
13
14 static int __exit lkp_exit(void)
15 {
16 printk("<2>Hello,Word exit\n");
17 }
18 module_init(lkp_init);
19 module_exit(lkp_exit);

²¢ÔÚhello.cµÄͬһĿ¼Ï½¨Á¢MakefileÎļþ¡£
MakefileÎļþ£º
1 ifneq ($(KERNELRELEASE),)
2 mymodule-objs:= hello.c
3 obj-m += hello.o
4
5 else
6 PWD :=$(shell pwd)
7 KVER := $(shell uname -r)
8 KDIR :=/lib/modules/$(KVER)/build
9
10 all:
11 $(MAKE) -C $(KDIR) M=$(PWD)
12 clean:
13 rm -rf *.o *.mod.c *.ko *.symvers *order *.markers *-
14 endif
˵Ã÷£º
µ±ÃüÁîÔÚÖ´ÐÐmakeʱ£¬½«µ÷ÓÃMakefileÎļþ¡£KERNELRELEASE ÊÇÔÚÄÚºËÔ´´úÂëµÄ¶¥²ã/usr/src/linux-headers-2.6.32-33/Makefile ÎļþÖж¨ÒåµÄÒ»¸ö±äÁ¿¡£¶ÁÕßÈç¹ûÏÂÔصÄÊÇlinux-3.0µÄÄں˵Ļ°ÔÚ**/linux-3.0/Makefile¡£µ±ÖеÄ380ÐС£ÈçÏ£º

379 # Read KERNELRELEASE from include/config/kernel.release (if it exists)
380 KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)
381 KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL) ,.$(SUBLEVEL)))$(EXTRAVERSION)
ÔÚµÚÒ»´Î¶ÁÈ¡Ö´ÐдËMakefileʱ£¬±äÁ¿$(KERNELRELEASE) ²¢Ã»Óб»ÉèÖã¬Òò´ËµÚÒ»ÐÐifneqµÄÌõ¼þʧ°Ü£¬´ÓelseºóÃæµÄ¿ªÊ¼Ö´ÐУ¬ÉèÖÃPWD¡¢KVER¡¢KDIRµÈ±äÁ¿¡£
µ±makeÓöµ½±êºÅallʱ£¬-C$(KDIR)Ö¸ÃûÌøתµ½ÄÚºËÔ´ÂëĿ¼Ï¶ÁÈ¡ÄÇÀïµÄMakefile¡£M=$(PWD)£¬±íÃ÷·µ»Øµ½µ±Ç°µÄĿ¼½øÐмÌÐø¶ÁÈ룬ִÐе±Ç°µÄMakefile£¬Ò²¾ÍÊǵڶþ´Îµ÷ÓÃmake¡£ÕâʱµÄ±äÁ¿$(KERNELRELEASE) ÒѾ­±»¶¨Ò壬Òò´Ëifneq³É¹¦£¬make½«¼ÌÐø¶ÁÈ¡½ô½ÓÔÚifneqºóÃæµÄÄÚÈÝ¡£ifneqµÄÄÚÈÝΪkbuildÓï¾ä£¬Ö¸ÃûÄ£¿éÔ´ÂëÖиöÎļþÖ®¼äµÄÒÀÀµ¹ØϵºÍÒªÉú³ÉµÄÄ¿±êÄ£¿é
Óï¾ä 2 mymodule-objs:= hello.c ±íÊÇmymodule.o ÓÉhello.cÉú³É¡£ Óï¾ä3 obj-m += hello.o±íÊÇÁ´½Óºó½«Éú³Émymodule.koÄ£¿é£¬Õâ¸öÎļþ¾ÍÊÇÒª²åÈëÄں˵ÄÄ£¿éÎļþ¡£
Èç¹ûmakeµÄÄ¿±êÊÇclean£¬Ö±½ÓÖ´ÐÐclean±êºÅÖ®ºóµÄ²Ù×÷£¬É¾³ý Ò»´ó¶ÑµÄ¶«Î÷£¡Ö´ÐÐÎÄcleanºóÃæµÄrmÃüÁîÖ®ºó£¬makeÕû¸ö¹¤×÷¾Í½áÊøÁË£¡

Ö´ÐÐЧ¹û£º
[#35#caopeng@laptop:~]$cd kernel/
[#36#caopeng@laptop:~/kernel]$ls
hello.c Makefile
[#37#caopeng@laptop:~/kernel]$make
make -C /lib/modules/2.6.32-33-generic/build M=/home/caopeng/kernel
make[1]: ÕýÔÚ½øÈëĿ¼ `/usr/src/linux-headers-2.6.32-33-generic'
LD /home/caopeng/kernel/built-in.o
CC [M] /home/caopeng/kernel/hello.o
/home/caopeng/kernel/hello.c: In function ¡®lkp_exit¡¯:
/home/caopeng/kernel/hello.c:17: warning: no return statement in function returning non-void
/home/caopeng/kernel/hello.c: In function ¡®__exittest¡¯:
/home/caopeng/kernel/hello.c:19: warning: return from incompatible pointer type
Building modules, stage 2.
MODPOST 1 modules
CC /home/caopeng/kernel/hello.mod.o
LD [M] /home/caopeng/kernel/hello.ko
make[1]:ÕýÔÚÀ뿪Ŀ¼ `/usr/src/linux-headers-2.6.32-33-generic'
[#38#caopeng@laptop:~/kernel]$
±àÒë³É¹¦£¡£¡
²é¿´µ±Ç°Ä¿Â¼ÏµÄÎļþ£¬»á·¢ÏÖ¶àÁ˺ܶණÎ÷£¡ÈçÏ£º
[#38#caopeng@laptop:~/kernel]$ls
built-in.o hello.ko hello.mod.o Makefile Module.symvers
hello.c hello.mod.c hello.o modules.order
[#39#caopeng@laptop:~/kernel]$
ÕâÀï²»ÖªµÀΪʲôÉú³ÉµÄÎļþ¶¼Ã»ÓÐxȨÏÞ£¡ÕâÀïÒªÓõ½µÄ¾ÍÊÇhello.ko¸øÆä¼ÓÉÏ¿ÉÖ´ÐÐȨÏÞ¡£²¢Ê¹ÓÃinsmod½«Æä²åÈ룡ȻºóÓÃÃüÁî²é¿´½á¹û£¡
[#47#caopeng@laptop:~/kernel]$sudo rmmod hello.ko
[#48#caopeng@laptop:~/kernel]$sudo insmod hello.ko
[#49#caopeng@laptop:~/kernel]$dmesg -c
klogctl: ²»ÔÊÐíµÄ²Ù×÷
[#50#caopeng@laptop:~/kernel]$sudo dmesg -c
[14651.331364] Hello,Word exit
[14658.553284] Hello ,
[#51#caopeng@laptop:~/kernel]$
×îºóÒª¼ÇµÃrmmod£¡£¡£¡
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 67 ÌõÆÀÂÛ

  1. ck1212_00 ÓÚ 2017-02-27 12:37:53·¢±í:

    ¸Ðл·ÖÏí£¡

  2. han19850618 ÓÚ 2016-12-29 11:34:28·¢±í:

    ¿´²»¶®ÁË

  3. Changlobx ÓÚ 2014-10-30 10:49:36·¢±í:

    ºÜ¸ßÐËÈÏʶ´ó¼Ò£¬Ï£ÍûÒ»Æðѧϰ£¬Ò»ÆðÌá¸ß£¬×ÊÔ´²»´í

  4. poiu125sd ÓÚ 2014-03-17 09:42:02·¢±í:

    ÐÂÊÖÕõ»ý·Ö£¬ÇëÁ½â

  5. Tron ÓÚ 2014-03-17 09:39:14·¢±í:

    ºÃÀý×Ó£¬¶àл·ÖÏí£¡

  6. hero_71 ÓÚ 2013-06-18 00:23:42·¢±í:

    »¹ÐаÉ

  7. linux_fzt ÓÚ 2013-06-12 10:23:27·¢±í:

    ÕâÀ£¬ÕâÀ£¬£¬·¢µãºÃ¶«Î÷¡£¡£¡£Î÷¶«Íø¡£¡£¡£httpÈ»ºóÊÇxidong.net£¬ËÑË÷¡±linuxÍøÂç±à³Ì¡°¡±linuxÄں˽²½â¡°¡±linuxÈëÃŽ̡̳°µÈÏà¹Ø×ÊÔ´£¬¾Í»áÕÒµ½ºÜ¶àºÜºÃµÄ×ÊÔ´£¬ÊÓƵ¡¢pdf¡¢mp3µÈµÈ¡£¡£×ÊÔ´·á¸»£¬ºÜºÃµÄÒ»¸öÍøÕ¾¡£¡£¡£¡£

  8. linux_fzt ÓÚ 2013-06-12 10:23:22·¢±í:

    ÕâÀ£¬ÕâÀ£¬£¬·¢µãºÃ¶«Î÷¡£¡£¡£Î÷¶«Íø¡£¡£¡£httpÈ»ºóÊÇxidong.net£¬ËÑË÷¡±linuxÍøÂç±à³Ì¡°¡±linuxÄں˽²½â¡°¡±linuxÈëÃŽ̡̳°µÈÏà¹Ø×ÊÔ´£¬¾Í»áÕÒµ½ºÜ¶àºÜºÃµÄ×ÊÔ´£¬ÊÓƵ¡¢pdf¡¢mp3µÈµÈ¡£¡£×ÊÔ´·á¸»£¬ºÜºÃµÄÒ»¸öÍøÕ¾¡£¡£¡£¡£

  9. linux_fzt ÓÚ 2013-06-12 10:23:18·¢±í:

    ÕâÀ£¬ÕâÀ£¬£¬·¢µãºÃ¶«Î÷¡£¡£¡£Î÷¶«Íø¡£¡£¡£httpÈ»ºóÊÇxidong.net£¬ËÑË÷¡±linuxÍøÂç±à³Ì¡°¡±linuxÄں˽²½â¡°¡±linuxÈëÃŽ̡̳°µÈÏà¹Ø×ÊÔ´£¬¾Í»áÕÒµ½ºÜ¶àºÜºÃµÄ×ÊÔ´£¬ÊÓƵ¡¢pdf¡¢mp3µÈµÈ¡£¡£×ÊÔ´·á¸»£¬ºÜºÃµÄÒ»¸öÍøÕ¾¡£¡£¡£¡£

  10. surarain ÓÚ 2013-05-29 09:19:33·¢±í:

    nice

  11. chunpengkai ÓÚ 2013-05-09 17:30:18·¢±í:

    ѧϰÖУ¬Ð»Ð»

  12. longshao221 ÓÚ 2013-05-03 14:59:31·¢±í:

    ²»´í

  13. shell10000 ÓÚ 2013-03-14 09:25:07·¢±í:

    лл·ÖÏí

  14. zshxiangni ÓÚ 2013-03-11 11:48:05·¢±í:

    ¸Ðл·ÖÏí

  15. shenlonghaha ÓÚ 2012-12-31 22:08:52·¢±í:

    Ãþ×Åʯͷ¹ýºÓ

  16. ÔÂÂä½ðÁêÃÎ ÓÚ 2012-12-14 16:16:49·¢±í:

    {:2_91:}

  17. pythonner ÓÚ 2012-12-13 10:31:10·¢±í:

    ¸Ðл·ÖÏí£¬Ñ§µ½ÖªÊ¶ÁË£¡

  18. freeny ÓÚ 2012-12-09 15:03:23·¢±í:

    ÊܽÌÁË£¬¸Ðл·ÖÏí£¬Ö§³Ö

  19. gky2009514 ÓÚ 2012-12-07 13:03:42·¢±í:

    ÐÂÊÖÍê³ÉÈÎÎñ£¬ÇóÏÂÊé¼®~

  20. alnnawang ÓÚ 2012-12-02 23:24:13·¢±í:

    лл¥Ö÷

  21. alnnawang ÓÚ 2012-12-02 23:21:42·¢±í:

    ²»´íµÄ

  22. sunlightboy514 ÓÚ 2012-12-02 22:56:07·¢±í:

    òËÆ´úÂëÖÐȱÉÙÁËMODULE_LICENSE£¨"GPL"£©ÉùÃ÷

  23. preooo ÓÚ 2012-11-24 21:45:08·¢±í:

    ²»´í

  24. linux004 ÓÚ 2012-11-23 09:37:47·¢±í:

    ¼á³Ö¾ÍÊÇʤÀû¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£

  25. kyan1991 ÓÚ 2012-07-26 22:17:25·¢±í:

    ÊÕ²ØÁË£¡£¡£¡£¡£¡£¡£¡£¡

  26. kyan1991 ÓÚ 2012-07-18 22:28:05·¢±í:

    ºÜỆܽ¬Çë½ÌÒ»ÏÂÄãÄں˴úÂëÔĶÁ£¬±à¼­ÓÃʲôÈí¼þ°¡£¿

  27. yt20090291 ÓÚ 2012-07-08 16:25:36·¢±í:

    лл·ÖÏí

  28. ÓÚ 2012-05-16 17:46:06·¢±í:

    ʵ¼ùÁËһϣ¬¼ÓÉîÁËÀí½â¡£Ð»Ð»¡£

  29. aslin08 ÓÚ 2012-05-16 14:52:58·¢±í:

    ºÜºÃµÄÀý×Ó£¬¸Ðл·ÖÏí

  30. 5714723 ÓÚ 2012-05-12 18:24:39·¢±í:

    лл¥Ö÷°¡£¡

  31. honglianqxw123 ÓÚ 2012-05-08 08:43:53·¢±í:

    ÊǸöºÃ·¶±¾£¬Ð»Ð»¡£

  32. honglianqxw123 ÓÚ 2012-05-07 07:27:03·¢±í:

    ·¹ý£¬Á˽⡣

  33. ¿ÚÍõµã ÓÚ 2012-05-04 09:41:24·¢±í:

    ÈëÃųÌÐò£¬ºÇºÇ

  34. conghung2012 ÓÚ 2012-05-03 20:19:37·¢±í:

    ±à³Ì¸ÉÂºÃºÃÌåÑéubuntuÕâ¸ö¹¤¾ß¸øÄã´øÀ´µÄ·½±ãºÍÃÀ¸Ð¾ÍÐÐÁË£¬Òª×öºÃ¹¤×÷£¬»¹ÊÇÈ¥windowsÏ¡£²»ÒªÕÛÌÚ¡£

  35. µÈºòµÄÈË ÓÚ 2012-05-01 22:35:25·¢±í:

    лл¥Ö÷À²

  36. tea789a ÓÚ 2012-05-01 17:33:32·¢±í:

    ºÃ¶«Î÷,±ØÐ붥!!!лл·ÖÏí!!!

  37. liru125504 ÓÚ 2012-04-12 15:14:08·¢±í:

    ѧϰÁË

  38. fj97897217 ÓÚ 2012-04-11 21:55:38·¢±í:

    ²»´í£¬¼òµ¥ Ò׶®£¡ ÎûÎû~~~

  39. ÔÆ·É1992 ÓÚ 2012-04-10 20:05:36·¢±í:

    quite good

  40. silentwind ÓÚ 2012-03-27 16:44:44·¢±í:

    ΪÁË Ë¢·Ö²ÅÁôµÄÑÔ