<a target="_blank" href="http://www.cnblogs.com/-clq/archive/2012/01/31/2333247.html">codeblocks 使用彙總</a>
內建本帖提到的所有更新檔,非官方,雙編譯器(vc9、mingw4.4.3)綠色版,解壓密碼:csdn
<< 由于內建vc9,是以請試用後24小時内删除!
這段時間比較忙,主要是學習code::blocks的代碼,并且為其送出更新檔。
這幾天我為cb送出了幾個更新檔,歡迎大家積極測試,特散分感謝!
--------------------------------------------------
http://blog.csdn.net/sdfgh2046/article/details/5812663
原文位址:http://blog.csdn.net/utensil/archive/2008/12/24/3593502.aspx
說明:
1)以下需要設定的地方均在settings->editor...彈出的對話框中。
2)不少指令都可針對目前行或選中的代碼塊,下文簡稱目前行或選中塊。
==日常編輯==
• 按住ctrl滾滾輪,代碼的字型會随你心意變大變小,對保護視力特别有好處。
• 在編輯區按住右鍵可拖動代碼,省去拉(尤其是橫向)滾動條之麻煩;相關設定:mouse drag scrolling。
• ctrl+d可複制目前行或選中塊。
• ctrl+shift+c注釋掉目前行或選中塊,ctrl+shift+x則解除注釋。
• tab縮進目前行或選中塊,shift+tab減少縮進。
• 可拖動選中塊使其移動到新位置,按住ctrl則為複制到新位置。
• 按下atl,再拖動滑鼠,可以實作部分選擇(即隻選中一個區域内的字元,而不會包含它們所在行的其他字元)。
• 需要更大編輯空間時,f2和shift+f2分别可以顯隐下方logs & others欄和左方的management欄。
==自動完成與縮寫==
1)優化代碼自動完成功能:在code-completion and symbol browser中,
• 将automatically launch when typed # letter中的4改成2,這樣打兩個字母就會有提示了。
• 将keyword sets to additionally include中1到9都勾上(可在syntax highlighting 的keywords...中設定,其中1是c++關鍵字,3是doxygen關鍵字;我曾将wxwidgets的類名都加入7并設定相應的字型(粗黑 體),看代碼時特别爽)
• 将delay for auto-kick-in when typing [.::->]拉到 200ms,這樣快點出來提示
• 選中case-sensitive match,防止一些無關的東西幹擾,如果你想它幫你糾正大小寫,那就去掉勾
• 在keyboard short-cuts中将edit->code complete的快捷鍵由ctrl+space改為alt+/,因為前者與中文輸入法切換沖突,該快捷鍵為已經輸入的(不是正在輸入的)詞提供自動完成。
2)看abbreviation一欄,裡面定義了許多縮寫(還可以自定義),隻要輸入這些縮寫,并按ctrl+j,就可以自動完成常用的代碼架構,并将光标放在恰當的地方(自定義時用|表達)。常用的有:guard、class、switch等。
3) 如果你聲明了一個類,你可以在cpp檔案中右擊,insert->all class methods without implementation...來插入你還沒定義的方法的定義(省去不少打字的功夫哦),也可使用insert->class method declaration/implementation...來插入一個方法的聲明或定義。
==導航相關==
• ctrl+g 到達指定行,alt+g 到達指定檔案,ctrl+alt+g 到達指定函數(支援頭檔案中的函數定義),f11 切換源檔案與頭檔案。
• ctrl+pageup 到達上一個函數,ctrl+pagedown 到達下一個函數。
• ctrl+b 添加書簽,alt+pageup和alt+pagedown可以切換書簽。
• ctrl+shift+b可找到比對的括号。
• 看長代碼時,可右擊,folding->fold all,然後慢慢展開來看,也可充分利用左方management欄的symbol浏覽器。
• 在一個變量、函數或宏上右擊,三個以find開頭的菜單項,分别可以為你轉到它的聲明、定義和找到所有出現的地方(按f2在下方thread search那裡檢視)。
其他:
• general settings中可以設定縮進、自動換行等細節。
• 讓code::blocks永遠記住你的layout,尤其是debug的layout,善用debug工具欄。
• 備份c:/documents and settings/[你的使用者名]/application data/codeblocks/default.conf,如遇重裝,将其放在codeblocks.exe所在目錄,就不會丢失你的配置;這樣也可以 打造出code::blocks的綠色版。
前面都是廢話。
打開code::blocks(采用中文語言包的界面),主菜單“設定” -> “編譯器與調試器” ,選中gcc編譯器。然後配置其編譯器選項:
enable effective-c++ warnings....
這樣配置,是讓所有以後在code::blocks建立的,采用gcc編譯器的c++項目,都會使用這個選項。如果你隻是想在個别項目中使用,那麼可以在具體項目中做此配置(打開工程之後,主菜單:項目->建構選項)。
有了這個選項,會帶來什麼好處呢?我們先考一下自己:
code:
#include <iostream>
using namespace std;
class person
{
public :
virtual void say()
{
cout << "i am a person." << endl;
}
~person()
cout << "bye-bye person." << endl;
};
class beauty : public person
beauty()
: _p (new int )
~beauty()
delete _p;
virtual void say()
cout << "i am a beauty." << endl;
private :
int * _p;
int main()
{
return 0;
}
以上代碼存在哪些設計上的隐患呢? 文法是肯定沒有問題的啦,一個沒有讀過書的編譯器,肯定會聽話地編譯完以上代碼。
但一個有讀過的書的編譯器,它會給出以下警告,帶上簡單翻譯:
||=== temp4book, debug ===|
main.cpp|7|warning: 'class person' has virtual functions and accessible non-virtual destructor
(第7行,class person擁有一個虛函數,可是它的析構函數不是虛的! 太可怕的設計錯誤了)
main.cpp|21|warning: 'class beauty' has pointer data members
(第21行,class beauty擁有一個指針資料成員,接下一行……)
main.cpp|21|warning: but does not override 'beauty(const beauty&)'
(但是,你沒有給beauty定制拷貝構造函數!)
main.cpp|21|warning: or 'operator=(const beauty&)'
(同樣,也沒有重載它的指派操作符!)
main.cpp|21|warning: 'class beauty' has virtual functions and accessible non-virtual destructor
(第21行,class beauty同樣擁有一個虛函數,可是它的析構函數不是虛的! 原因在其基類上的,呵呵。)
||=== 已完成建構: 0 個錯誤, 5 個警告 ===|
你不信這些警告的重要性嗎?可以買前面提到的那兩本書,國内有出版。這裡簡單一例:
void test()
beauty b1;
beauty b2=b1;
}
嚴重問題發生了,你能知道問題在哪嗎?來做一道試題測試一下吧:)
<a target="_blank" href="http://student.csdn.net/space.php?do=question&ac=detail&qid=1839">http://student.csdn.net/space.php?do=question&ac=detail&qid=1839</a>
第一次使用codeblocks,就遇到了中文問題的困擾 ,如下的寫法,在編譯時,出錯:
wxstring msg(_t( " 中華人民共和國 " ));
error:converting to execution character set: illegal byte sequence
呵,gcc在編譯時,無法将漢字進行正确的轉換,解決的辦法是,明确告訴gcc編譯器,輸入的檔案是中文的,這樣就可以了,設定編譯器參數,因為在 程式中使用中文,将是非常常見的,是以,我将設定全局的參數,如下:settings->compiler and debugger,如下圖:
設定編譯選項:-finput-charset=gbk
code::blocks最近新帶的插件工具:cppcheck。這是一款靜态檢查c++程式代碼的工具。和c::b一樣是一款開源的軟體。
cppcheck is an analysis tool for c/c++ code. unlike c/c++ compilers and many other analysis tools, we don't detect syntax errors. cppcheck only detects the types of bugs that the compilers normally fail to detect. the goal is no false positives.
cppcheck其實是一個獨立的工具,完全脫離任何ide可以運作,早先我就是獨立使用它,但自打c::b把它給內建了,感覺使用起來更是友善了不少。
新版的插件菜單中,可以找到cppcheck插件,不過事前你可能需要單獨下載下傳,安裝好cppcheck。下載下傳位址:
http://sourceforge.net/projects/cppcheck/files/
root 安裝
make & make install
為了示範,直接上手。搞一些低級錯誤,讓cppcheck檢查一下:
#include <fstream>
using namespace std;
#define max_int_item_count 100
int * p = new int [max_int_item_count]; //配置設定了記憶體
for (int i=0; i<max_int_item_count; ++i)
p[i] = max_int_item_count-i;
return 0; //沒釋放 p 就byebye了
運作插件,code::blocks 消息欄将顯示檢查結果:
說的是: 在main.cpp 的第17行,存在“記憶體洩漏的可能,相關對象是:p。”
呵呵,繼續改一下代碼:
//... 前面代碼略
delete p; //正确寫法應是:delete[] p;
return 0;
//...
不需編譯,再從c::b菜單裡運作一下cppcheck插件……這回是:
main.cpp|17|mismatchallocdealloc : error : mismatching allocation and deallocation: p|
意思是:在main.cpp檔案裡,第17行(上代碼片段中的09行),有個“不比對的記憶體配置設定與釋放”的錯誤,對象仍然是:p。(new[] 出來的東西,要用delete[]釋放,你非要說用delete也可以,那是因為你在用一個超級有問題的編譯器....)
注意事項:
a) 顯然,它不能檢查一切。
b)但有意思的,它經常能查出你自己都想不到問題。(偶爾讓你一身冷汗,連續120分鐘不敢再邊寫代碼邊斜眼看邊上的美女)
c)當然啦,有的它檢查出來了,但倒也不一定就是天條,畢竟c++程式是自由的,c++程式員個個都會搞些暗爽的代碼寫法。
結合 code::blocks的一些問題
很大的工程,或者工程有超大的檔案(往往是工具生成的那種),它檢查起來是超慢的,這時c::b會先是死等,慢慢的,就會變成等死。
windows下 codeblocks 10.05安裝配置
一、下載下傳安裝包
如果您有安裝以前版本,并不需要解除安裝。
請下載下傳圖中所示的連結:
二、安裝必讀!
1)、請選擇定制安裝!custom
2)、選中全部插件
3)、不看可能會後悔:修改安裝目标路徑。
這不是code::blocks的錯,而是 mingw gcc 的連結器 ln.exe 有個bug,不能連結位于帶空格或漢字路徑下的檔案。
-------------------------------------------------------------------------------------
餘下的步驟,和一般軟體安裝也就差不多了。
三、配置編譯環境
1)、 運作code::blocks,第一次運作會彈出選擇編譯器,請選中gcc 編譯器(通常是第一項)。
2)、 進入主界面後,主菜單 setting -> compiler and debugger ...
彈出對話框中,左邊選中第一項:global compiler setting.. 右邊最上面選中“gnu gcc compiler” (通常預設就是它)
其下選中“toolchain executables”, 點一下 auto-detect,會讓c::b自動檢測到mingw的安裝路徑(通常不用檢查也是對的,mingw如按上述步驟安裝,它就在code::blocks的安裝路徑下)。
進入mingw的安裝路徑下的bin子目錄,比如:c:/codeblocks/mingw/bin.找一下是否有 “mingw32-make.exe”這個檔案,如果沒有,找到make.exe,複制一份,将複制品改名為 mingw32-make.exe。
( 同樣方法,找一下是否有 mingw32-gcc.exe ,ming32-g++.exe兩個程式,如果沒有,分别複制gcc.exe 及 g++.exe,将複制品改名為 mingw32-gcc.exe 及mingw32-g++.exe。 我忘了到底有沒有,大家就确認一下吧,有答案的話留言一下)
3) 同一對話框内,左邊切換到最後一項 “debugger setting”
右邊最上面的“debugger intialization commands”下,輸入 : handle sigtrap noprint
這對調試某些帶有調試資訊的windows sdk庫,有好處,否則調試器會不斷停在作業系統的某些庫的彙編代碼上....
同一頁面,編輯框往下的多選框,除了最後一項明顯不能選中以外,其它的都可以酌情選中(具體意思大家看一下文字也能了解,不能了解的話,可以安裝上中文語言包以後,再檢視)。
四、測試
點選主菜單:new -> project (或者直接在 start here 頁面上點選“create a new project”)。 對話框中左邊選中“project”,右邊在一堆圖示中找到“console application”。點選“go”開始向導。
向導第一步是歡迎頁,進入下一步,選“c++”。
下一個頁面中, project title 下輸入 hellocb ,不要帶空格。folder to create project in :中,通過末尾的小按鈕,選擇你要存儲工程的父路徑。同樣不要帶空格,比如建立一個c:/mycppcode 目錄。
再下一步中,不需要修改,通常選中的就是gnu gcc編譯器了,并且預設選中要生成debug和release兩個建構目标,如名所示,一者用于調試,一者用于釋出。
點選 finish……打開項目樹中的“main.cpp”檔案。如果看不到項目樹,請按 shift + f2。
這是main.cpp裡,預設的代碼:
cout << "hello world!" << endl;
return 0;
沒錯,就是著名的hello world! 測試例程。按ctrl + f9 ,編譯之。如果一切設定無誤,編譯應能成功。要看編譯資訊,請按f2確定日志面闆出現。 編譯成功後,按 f9 運作……
下載下傳及使用我準備的最新中文語言包,以及如何做一些更好用的配置,下一節再講。
五、重要補充
windows vista , windows 7 使用者必讀:
1), 您最好以administrator 使用者安裝,使用code::blocks。
2), 如果想要編寫,以及調試 nt service之類的程式,最好設定 codeblocks.exe 的相容性為 “運作在administrator身份”這下。當然這樣設定之後,在win 7 等系統下,每次運作codeblocks,都要搞一下那個uac對話框了。
linux,mac 使用者,前述下載下傳頁,已經有提供多個發行版本的linux下載下傳連結,請自行去官網下載下傳。
高手參看: c::b支援多種編譯器,如果您有需要,請自行配置。 如果您有興趣自編譯新版c::b,可以下載下傳源碼,然後使用以前的8.02版加每夜建構更新包,從舊c::b裡編譯出一個新版c::b。
gui應用、wxwidgets應用、wxsmith工程,另外它還支援使用者自定義工程模闆。在wxwidgets應用中選擇unicode支援中文。
code::blocks支援文法彩色醒目顯示,支援代碼完成(目前正在重新設計過程中)支援工程管理、項目建構、調試。
code::blocks支援插件,目前的插件包括代碼格式化工具astyle;代碼分析器;類向導;代碼補全;代碼統計;編譯器選擇;複制字元串到剪 貼闆;調試器;檔案擴充處理器;dev-c++ devpak更新/安裝器;dragscroll,源碼導出器,幫助插件,鍵盤快捷鍵配置,插件向導;to-do清單;wxsmith;;wxsmith mime插件;wssmith工程向導插件;windowsxp外觀。
code::blocks具有靈活而強大的配置功能,除支援自身的工 程檔案、c/c++檔案外,還支援angelscript、批處理、css檔案、d語言檔案、diff/patch檔案、fortan77檔案、 gamemonkey腳本檔案、 hitachi彙編檔案、lua檔案、masm彙編檔案、mathlab檔案、nsis開源安裝程式檔案、ogre compositor腳本檔案、ogre material腳本檔案、opengl shading語言檔案、python檔案、windows資源檔案、xbase檔案、xml檔案、nvidia
cg檔案。識别dev-c++工程、ms vs 6.0-7.0工程檔案,工作空間、解決方案檔案。
codeblocks插件介紹
衆所周知cb的一個優點就是開放式架構提供靈活的插件開發和支援,我們也相信在今後的發展上插件對cb的貢獻會越來越大。
下面是現有核心插件和貢獻插件的簡介清單:
the core plugins are installed by default and offer the basic functions of code::blocks. the core plugins are maintained / developed by the official development team.
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=autosave_plugin&action=edit">autosave</a>
<dl><dd>saves project files between intervals.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=class_wizard_plugin&action=edit">class wizard</a>
<dl><dd>provides wizard for creating new classes.</dd></dl>
and class browser.
<dl><dd>provides support for various compilers in one interface.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=debugger_plugin&action=edit">debugger</a>
<dl><dd>provides support for various debuggers in one interface.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=file_extension_handler_plugin&action=edit">file extensions handler</a>
<dl><dd>adds extra file extension handlers.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=open_files_list_plugin&action=edit">open files list</a>
<dl><dd>manages a list of all opened files (editors).</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=projects_importer_plugin&action=edit">projects importer</a>
<dl><dd>imports projects from other ide’s, e.g. ms visual studio and devc++.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=scripted_wizard_plugin&action=edit">scripted wizard</a>
<dl><dd>provides scripted wizard functionality.</dd></dl>
<dl><dd>formats source code files with specific style.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=to-do_list_plugin&action=edit">to-do list</a>
<dl><dd>adds to-do items to source code.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=winxp_look%27n%27feel_plugin&action=edit">winxp look’n'feel</a>
<dl><dd>creates manifest file which enables the version 6.0 of the common controls on windows xp.</dd></dl>
the user-contrib uted plugins are not installed by default and offer extended functionality for code::blocks. the contrib plugins are maintained / developed by third-party developers.
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=autoversioning">auto versioning</a>
<dl><dd>helps you keep track of your project version and status.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=browse_tracker_plugins">browse tracker</a>
<dl><dd>browse to previous source positions.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=c::b_games_plugin">c::b games</a>
<dl><dd>games in a integrated development environment? you bet.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=code_profiler_plugin&action=edit">code profiler</a>
<dl><dd>provides graphical interface to gnu gprof profiler.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=code_snippets_plugin">code snippets</a>
<dl><dd>manages small pieces of code (i.e. snippets).</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=code_statistics_plugin">code statistics</a>
<dl><dd>shows various statistics from source code files.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=copy_strings_to_clipboard_plugin&action=edit">copy strings to clipboard</a>
<dl><dd>copies literal strings from the current editor to clipboard.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=devpak_installer_plugin&action=edit">devpak installer</a>
<dl><dd>installs and updates devc++ devpaks.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=dragscroll_plugin">dragscroll</a>
<dl><dd>enables dragging and scrolling with mouse.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=environment_variables_plugin">environment variables</a>
<dl><dd>sets environment variables within the focus of code::blocks.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=help_plugin">help plugin</a>
<dl><dd>integrates third-party help files to the interface.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=hexeditor_plugin&action=edit">hexeditor plugin</a>
<dl><dd>opens files in a code::blocks integrated hexeditor.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=keyboard_shortcuts_plugin">keyboard shortcuts</a>
<dl><dd>manages menu shortcuts.</dd></dl>
<dl><dd>queries the koders webpage for keywords.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=keymacs_plugin">keymacs plugin</a>
recording, playback, and editing of keystroke macros.
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=regex_testbed_plugin&action=edit">regex testbed</a>
<dl><dd>regular expressions testbed.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=source_exporter_plugin&action=edit">source exporter</a>
<dl><dd>exports source code files to other formats such as html and pdf.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=symbol_table_plugin&action=edit">symbol table</a>
<dl><dd>a simple graphical interface to the gnu symbol table displayer (nm).</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=threadsearch">threadsearch</a>
<dl><dd>multi-threaded ‘search in files’ with preview window.</dd></dl>
<a target="_blank" href="http://wiki.codeblocks.org/index.php?title=valgrind_plugin&action=edit">valgrind</a>
<dl><dd>valgrind analysis tools integration.</dd></dl>
<dl><dd>rad tool for creating wxwidgets dialogs.</dd></dl>