⑴ 如何通過linux源碼樹製作linux kernel headers用於內核模塊開發
先查看自己OS使用的內核版本
shana@shana:~$ uname -r
2.6.22-14-generic
如果安裝系統時,自動安裝了源碼。在 /usr/src 目錄下有對應的使用的版本目錄。例如下(我是自己下的)
shana@shana:/usr/src$ ls
linux-headers-2.6.22-14
linux-headers-2.6.22-14-generic
linux-source-2.6.22
linux-source-2.6.22.tar.bz2
shana@shana:/usr/src$
如果沒有源碼。(一般ubuntu 都沒有吧)
查看一下可一下載的源碼包(切記不要使用超級用戶使用此命令否則……會提示沒有此命令)
shana@shana:/usr/src$ apt-cache search linux-source
linux-source - Linux kernel source with Ubuntu patches
xen-source-2.6.16 - Linux kernel source for version 2.6.17 with Ubuntu patches
linux-source-2.6.22 - Linux kernel source for version 2.6.22 with Ubuntu patches
shana@shana:/usr/src$
我選擇了 linux-source-2.6.22 - Linux kernel source for version 2.6.22 with Ubuntu patches 這個~
然後 install 之
shana@shana:/usr/src$ sudo apt-get install linux-source-2.6.22
下載完成後,在/usr/src下,文件名為:linux-source-2.6.22.tar.bz2,是一個壓縮包,解壓縮既可以得到整個內核的源代碼:
注意 已經切換到超級用戶模式
root@shana:/usr/src#tar jxvf linux-source-2.6.20.tar.bz2
解壓後生成一個新的目錄/usr/src/linux-source-2.6.22,所有的源代碼都在該目錄下。
進入該目錄
開始配置內核 選擇最快的原版的配置(默認)方式 (我是如此)
root@shana:/usr/src/linux-source-2.6.22# make oldconfig
當然你也可以使用 自己喜歡的配置方式 如 menuconfig , xconfig(必須有GTK環境吧)。反正不用剪裁什麼,所以不管那種方式能配置它就行了。
完成後,開始make 吧 這兒比較久 一般有1一個小時吧。(保證空間足夠 我編譯完成後 使用了1.8G) 我分區時分給/目錄30G的空間,我沒遇到這問題。倒是我朋友遇到了。
shana@shana:/usr/src/linux-source-2.6.22$ make
shana@shana:/usr/src/linux-source-2.6.22$ make bzImage
當然,第一個make也可以不執行,直接make bzImage。執行結束後,可以看到在當前目錄下生成了一個新的文件: vmlinux, 其屬性為-rwxr-xr-x。
然後 :
root@shana:/usr/src/linux-source-2.6.22#make moles
root@shana:/usr/src/linux-source-2.6.22#make moles_install
執行結束之後,會在/lib/moles下生成新的目錄/lib/moles/2.6.22-14-generic/
。 在隨後的編譯模塊文件時,要用到這個路徑下的build目錄。至此,內核編譯完成。可以重啟一下系統。
至此 內核樹就建立啦
⑵ 有個 c 文件,如何才能將其編譯成動態鏈接庫
有二個文件,一個 test.h, 一個 test.c
//test.h
#pragmaonce
#ifdef__cplusplus
extern"C"{
#endif
intadd(inta,intb);
#ifdef__cplusplus
}
#endif
//test.c
#include"test.h"
intadd(inta,intb)
{
returna+b;
}
編譯,生成動態鏈接庫:
#gcc-g3-Wall-fPIC-shared-olibtest.sotest.c
#ls-l
-rwxr-xr-x1rootroot14618Jul2413:44libtest.so
-rw-r--r--1rootroot63Jul2413:43test.c
-rw-r--r--1rootroot106Jul2413:43test.h
#nmlibtest.so
00000000002006e8a_DYNAMIC
0000000000200890a_GLOBAL_OFFSET_TABLE_
w_ITM_deregisterTMCloneTable
w_ITM_registerTMCloneTable
w_Jv_RegisterClasses
00000000002006c8d__CTOR_END__
00000000002006c0d__CTOR_LIST__
00000000002006d8d__DTOR_END__
00000000002006d0d__DTOR_LIST__
00000000000006b8r__FRAME_END__
00000000002006e0d__JCR_END__
00000000002006e0d__JCR_LIST__
00000000002008b8d__TMC_END__
00000000002008b8A__bss_start
w__cxa_finalize@@GLIBC_2.2.5
0000000000000620t__do_global_ctors_aux
0000000000000550t__do_global_dtors_aux
00000000002008b0d__dso_handle
w__gmon_start__
00000000002008b8A_edata
00000000002008c8A_end
0000000000000658T_fini
0000000000000468T_init
0000000000000600Tadd
00000000000004a0tcall_gmon_start
00000000002008b8bcompleted.6557
00000000000004c0tderegister_tm_clones
00000000002008c0bdtor_idx.6559
00000000000005d0tframe_mmy
0000000000000500tregister_tm_clones