??時(shí)光流逝,記得十年前還是個(gè)游手好閑的少年,無聊時(shí)覺得應(yīng)該分析一下Linux內(nèi)核源碼,沒有別的動(dòng)機(jī),只覺得這樣很酷。
??沒有任何項(xiàng)目經(jīng)驗(yàn)的話,突然眼前出現(xiàn)一個(gè)浩大的工程,除了驚嘆之余,再無其他可言。Linux內(nèi)核的學(xué)習(xí)曲線可能陡峭了一點(diǎn),如果沒有點(diǎn)定力的話,是不會(huì)有什么進(jìn)步的。
??當(dāng)把幾十MB的tar包下載到本地后,打開方式便成了困擾我的一個(gè)問題,前前后后使用過好多工具,但都有一些缺點(diǎn)。
Vim
各種插件,各種配置。vim如果定義為IDE的話,那還屬于輕量級(jí)的IDE,通過Nerdtree+taglist+ctags+ycm這些大眾一點(diǎn)的插件,完全可以實(shí)現(xiàn)語法高亮,代碼跳轉(zhuǎn)等IDE具備的功能,但對(duì)于SLOC大于100K數(shù)量級(jí)的工程貌似有點(diǎn)力不從心。相對(duì)與大型IDE來說,它更像一把短小而鋒利的瑞士軍刀。
Emacs
神用的編輯器,學(xué)習(xí)曲線是盤旋型的,高深莫測。太難駕馭,對(duì)與我這種凡人,可能一輩子都盤旋在這個(gè)漩渦里。
Source Insight
看過好多教程使用這東東瀏覽Linux內(nèi)核,總是覺得很別扭,Linux自己家的事,為什要放在Windows上,文件名不區(qū)分大小寫這一點(diǎn)就足以讓人頭疼了。
LXR
例如: http://elixir.free-electrons.com/linux/latest/source 非常適合各種版本的Linux內(nèi)核代碼間比較和瀏覽,但是你想修改一下再編譯一下就沒有這功能了。
??進(jìn)行過一下嘗試后,還是覺得需要一個(gè)相對(duì)重量級(jí)的IDE來瀏覽Linux內(nèi)核代碼比較省時(shí)省力,比如Eclipse,QTCreator,Netbeans. 我這里暫且使用Eclipse來搭建一個(gè)瀏覽Linux內(nèi)核的環(huán)境,以幫助新人少走彎路。
在使用eclipse創(chuàng)建工程之前需要兩步準(zhǔn)備工作:1,準(zhǔn)備好交叉編譯工具配置好環(huán)境變量;2,將Linux Kernel下載解壓并成功編譯一次。
下面這段輸出是使用命令
export ARCH=arm CROSS_COMPILE=arm-linux-
make arm_vexpress_defconfig
make zImage V=1
輸出的 ./init/main.c 文件的編譯參數(shù),這段參數(shù)將作為我們接下來配置Eclipse的重要參考。
arm-linux-gcc -Wp,-MD,init/.main.o.d -nostdinc -isystem /opt/arm-buildroot-linux-uclibcgnu/usr/lib/gcc/arm-buildroot-linux-uclibcgnueabihf/5.4.0/include -I/mnt/sda5/workplace/linux-3.10.107/arch/arm/include -Iarch/arm/include/generated -Iinclude -I/mnt/sda5/workplace/linux-3.10.107/arch/arm/include/uapi -Iarch/arm/include/generated/uapi -I/mnt/sda5/workplace/linux-3.10.107/include/uapi -Iinclude/generated/uapi -include /mnt/sda5/workplace/linux-3.10.107/include/linux/kconfig.h -D__KERNEL__ -mlittle-endian -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -fno-delete-null-pointer-checks -std=gnu89 -O2 -fno-dwarf2-cfi-asm -fno-ipa-sra -mabi=aapcs-linux -mno-thumb-interwork -funwind-tables -marm -D__LINUX_ARM_ARCH__=7 -march=armv7-a -msoft-float -Uarm -Wframe-larger-than=1024 -fno-stack-protector -Wno-unused-but-set-variable -fomit-frame-pointer -fno-var-tracking-assignments -g -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fconserve-stack -DCC_HAVE_ASM_GOTO -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(main)" -D"KBUILD_MODNAME=KBUILD_STR(main)" -c -o init/main.o init/main.c
下面我們開始通過Eclipse構(gòu)建Linux Kernel 工程,我使用的版本是Eclipse cdt neon 3
打開Eclipse工程后 File–>New–>Other–>C/C++–>Makefile Project with Existing Code 然后將Kernel所在目錄配置好
配置好工程目錄后,右鍵Project Explorer 中的工程,選擇 Properties 進(jìn)入工程屬性配置窗口。
選擇C/C++ build ,將 Use default build command選項(xiàng)去掉,在Build command中輸入 ARCH 和 CROSS_COMPILE 的配置信息,配置如下圖
配置 C/C++ Gernal ,這一項(xiàng)中配置信息較多。
首先選擇 indexer 子選項(xiàng),勾選 Enable project specific settings 選項(xiàng) 并 去掉 Index source files not included in the build 選項(xiàng)。
配置 Preprocessor Include Paths, Macros etc. 子選項(xiàng)。
其中 Entries 選項(xiàng)卡 選中 GNU C –> CDT User Setting Entries –> Add –> Preprocessor Macros File –> 選擇 ./include/gernerated/autoconf.h 文件 –> OK
所有make menuconfig 時(shí)的編譯配置信息都在這個(gè)文件里,所以需要讓eclipse識(shí)別這些信息。
完成。
聯(lián)系客服