使用bakefile编译C工程代码
生活随笔
收集整理的這篇文章主要介紹了
使用bakefile编译C工程代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、前言
最近有個想法,想把 ineedle 整體架構從頭自己編寫代碼來實現一下,來加深對iNeedle系統的理解,同時加強Linux + C相關知識。由于iNeedle系統的龐大,只能是先把框架搭起來,根據某些功能再往里邊添加東西。首先遇到的問題就是每寫一部分代碼都要進行調試,既不想使用gcc獨立的命令來進行調試,這樣代碼多了或路徑復雜就難以控制;又不想使用iNeedle原版的編譯文件,于是自己按照舊版本抽取出需要編譯iNeedle系統的腳本代碼來。這個腳本用來編譯iNeedle項目,主要是利用了bakefile工具。bakefile是一個跨平臺的自動生成makefile的開源工具,需要在項目中的每個子目錄中指定***.bkl配置文件(該配置文件指定需要編譯哪些文件,指定頭文件等),bakefile利用每個子目錄中bkl文件來生成相應的makefile文件;然后在主目錄(編譯文件所在目錄)中生成主makefile文件,由主makefile直接進行系統編譯,生成可執行程序。
1、如果CentOS系統安裝:
yum install bakefile -y2、如果debian或ubuntu系統:
需要源碼安裝,可以到官網下載bakefile-0.2.9.tar.gz版本的即可:
二、bakefile使用
bakefile -f gnu "$folder.bkl" -o "$folder.mk" >/dev/null 2>&1單獨使用使用make命令進行編譯:
make -f ineedle.mk install三、bkl文件參考
1 <?xml version="1.0"?>2 <makefile>3 <template id="posT">4 <include>../include/pcap</include>5 <include>../include/</include>6 <include>./</include>7 <sources>pos/pos_lib.c</sources>8 <sources>pos/pos_var.c</sources>9 <sources>pos/db_var.c</sources> 10 <sources>pos/pos_db.c</sources> 11 <sources>pos/hk.c</sources> 12 <sources>pos/hashfunctions.c</sources> 13 <sources>pos/md5.c</sources> 14 <sources>pos/ios_time.c</sources> 15 <sources>pos/pos_ut.c</sources> 16 <sources>pos/ios_sparse.c</sources> 17 <sources>pos/pos_lock.c</sources> 18 <sources>pos/iweb_api.c</sources> 19 <sources>pos/db_clean.c</sources> 20 <sources>pos/pos_mem.c</sources> 21 </template> 22 <exe id="pos_obj" template="posT"></exe> 23 </makefile>四、iNeedle系統編譯腳本
#!/bin/bash # # 這個編譯文件是從老版本compile.dat中提取出來單獨編譯ineedle的,目的是用來調試ineedle使用的。 # project_name="ineedle" project_modl="root shell pos cfg ilog session sha timer traf filter istat lex monitor cron http report diskdb dll alarm snmp persist system" project_shuc="nd" project_targ="ineedle" project_cmpa="-L../lib/linux -lpcap -lmysqlclient -lhasp_linux_96828 -rdynamic -ldl -lnetsnmp" project_macr="DBG DBG2 INEEDLE_ROOT NCURSES _INEEDLE_AMON _INEEDLE_USTAT _INEEDLE_URL _INEEDLE_CIP _INEEDLE_WEBDELAY _INEEDLE_AMON_ABN _INEEDLE_ALARM _INEEDLE_ELOG _INEEDLE_POST _FILE_OFFSET_BITS=64 _LARGEFILE_SOURCE _LARGEFILE64_SOURCE "compileFlag="debug" splitter="------------------------------------------------------" SECFLAG=`expr 0`function show_copyright() {tput cleartput smsoecho $splitterecho " iNeedle Compiler ©right dztec.cn "echo $splitterechotput rmso }function loopmodules() {modules=$project_modlecho $spiltterecho "generating module files......"echo $spiltterfor folder in $modules;do#echo $folderfolder=`expr $folder | tr -d ' '`filepath="$folder/$folder.bkl"#echo $filepathif [ ! -r "$filepath" ];thenecho -n -e "file: '$filepath' doesnot exit!"tput blinkecho -e "\tPlz check the file"tput sgr0SECFLAG=`expr 1`continueficd "$folder"folderlen=`expr length $folder`if [ $folderlen -lt 3 ];thenecho -n -e "generating $folder.mk ...... \t\t\t\t"elseecho -n -e "generating $folder.mk ...... \t\t\t"fiif ! bakefile -f gnu "$folder.bkl" -o "$folder.mk" >/dev/null 2>&1 ;thentput smsotput smulecho "[FAILED]"cd "../"tput rmsotput sgr0SECFLAG=`expr 1`continuefitput blinktput smulecho "[OK]"tput sgr0cd "../"done }function genemake() {target=ineedlecompileMacro=$project_macrtargetMakeFile="ineedle.mk"modules=$project_modlcompile_arg=$project_cmpaif [ $compileFlag == "debug" ];thenCFLAGS="CFLAGS=-g"release="-g"for flg in $compileMacrodoCFLAGS="$CFLAGS -D$flg"donefirm $targetMakeFile >/dev/null 2>&1echo "TARGET = $target" >> $targetMakeFileecho "$CFLAGS" >> $targetMakeFileHEAD=""for folder in $modulesdoFOLD=`expr $folder | tr a-z A-Z`HEAD="$HEAD \$("$FOLD"_OBJ_OBJECTS)"echo "include $folder/$folder.mk" >> $targetMakeFiledoneecho >> $targetMakeFileecho "install:$target">>$targetMakeFileecho >> $targetMakeFileecho "$target:$HEAD">>$targetMakeFileecho -e -n "\t\$(CC)$HEAD $release -o $target $compile_arg" >>$targetMakeFile}function parseerror() {echo $splitterwarnings=`cat .temp | grep -E "warning" | wc -l`tput smsoecho -n "WARNINGS: "tput rmsoecho $warningstput smsoecho -n "ERRORS: "echo "--"tput rmsoechocat .temp | grep -E -i "cannot|multiple|undefined|Error|Stop" | sed 's/.*\/\(.*\/.*\)/\1/'echo $splitter }function move_compile_files() {target="ineedle"mv *.o ../obj >/dev/null 2>&1mv *.d ../obj >/dev/null 2>&1mv ./$target ../release/ >/dev/null 2>&1chmod u+s ../release/$targetchmod g+s ../release/$target }function compile() {target=ineedletargetMakeFile=ineedle.mkif make -f $targetMakeFile install >.temp 2>&1 ;thenecho $splittertput smsoecho "Compiled successfully!"tput rmsoecho $splitterechoelseparseerrortput smsoecho "Compiling failed!"tput rmsoecho $spiltterficat .temp | grep -V ".mk\|gcc -c -o" >.warning }function run() {pnn="ineedle"tput smsoecho $splittertput rmsotput smsoecho "compiling $pnn......"tput rmsotput smsoecho $splittertput rmsoloopmodulesif [ $SECFLAG == 1 ]; thenecho $splittertput smsoecho "error happened"tput rmsoecho $splitterexitfigenemakecompilemove_compile_files }function load_header_file() {if [ -r "./ineedle-linux.h" ];thencp ineedle-linux.h ineedle.hfi }load_header_file show_copyright run總結
以上是生活随笔為你收集整理的使用bakefile编译C工程代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 潍坊旅游景点(山东潍坊这10座山独具特色
- 下一篇: 房子建设图纸设计图(房子设计图纸)